Skip to content

Commit

Permalink
🍔
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Sep 3, 2024
1 parent 82f1ac1 commit e49c533
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ Type: `function (): boolean`

#### [choice](https://github.com/transitive-bullshit/random/blob/fcead1d157da7b3551e25b1ba8d4b0787d608f07/src/random.ts#L229)

Returns an item chosen uniformly at trandom from the given array.
Returns an item chosen uniformly at random from the given array.

Convence wrapper around `random.uniformInt()`

Expand Down
7 changes: 2 additions & 5 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface ICacheEntry<T> {
* @name Random
* @class
*
* @param {RNG|function} [rng=Math.random] - Underlying the default, built-in `Math.random` pseudorandom number generator.
* @param {RNG|function|string|number} [rng=Math.random] - Underlying the default, built-in `Math.random` pseudorandom number generator.
*/
export class Random {
protected _rng!: RNG
Expand All @@ -69,7 +69,6 @@ export class Random {
* @see RNG.clone
*
* @param {string} [seed] - Optional seed for new RNG.
* @param {object} [opts] - Optional config for new RNG options.
* @return {Random}
*/
clone(rng: SeedType = this.rng.clone()): Random {
Expand All @@ -91,8 +90,6 @@ export class Random {
* random.use(seedrandom('kittens'))
* // or
* random.use(Math.random)
*
* @param {...*} args
*/
use(rng?: SeedType) {
this._rng = RNGFactory(rng)
Expand Down Expand Up @@ -204,7 +201,7 @@ export class Random {
}

/**
* Returns an item chosen uniformly at trandom from the given array.
* Returns an item chosen uniformly at random from the given array.
*
* Convence wrapper around `random.uniformInt()`
*
Expand Down
34 changes: 32 additions & 2 deletions src/seed.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import seedrandom from 'seedrandom'
import { expect, test } from 'vitest'
import { assert, expect, test } from 'vitest'

import random, { Random } from '../src/random'

Expand Down Expand Up @@ -37,6 +37,36 @@ test('Random constructor', () => {
const rng3 = new Random(Math.random)
expect(rng3).toBeDefined()

const rng4 = new Random('example--seed-string')
const rng4 = new Random('example-seed-string')
expect(rng4).toBeDefined()
})

test('random seed consistency', () => {
let value = 0
for (let i = 0; i < 10; ++i) {
const rng = new Random(seedrandom('my-seed'))
if (i <= 0) {
value = rng.float()
} else {
assert.equal(value, rng.float())
}
}

for (let i = 0; i < 10; ++i) {
const rng = new Random('my-seed')
if (i <= 0) {
value = rng.float()
} else {
assert.equal(value, rng.float())
}
}

for (let i = 0; i < 10; ++i) {
const rng = new Random(42)
if (i <= 0) {
value = rng.float()
} else {
assert.equal(value, rng.float())
}
}
})

0 comments on commit e49c533

Please sign in to comment.