diff --git a/src/distributions/normal.test.ts b/src/distributions/normal.test.ts index 51b6a38..483f39a 100644 --- a/src/distributions/normal.test.ts +++ b/src/distributions/normal.test.ts @@ -1,6 +1,7 @@ import seedrandom from 'seedrandom' import { assert, test } from 'vitest' +import type { SeedType } from '../types' import { RNGFunction } from '../../src/generators/function' import { RNGMathRandom } from '../../src/generators/math-random' import { RNGXOR128 } from '../../src/generators/xor128' @@ -15,7 +16,7 @@ test('random.normal() produces numbers', () => { } }) -const meanN = (t: T) => { +const meanN = (t: T) => { const r = random.clone(t) const d = r.normal(120) let sum = 0 diff --git a/src/random.ts b/src/random.ts index e63905a..17c2aab 100644 --- a/src/random.ts +++ b/src/random.ts @@ -1,4 +1,5 @@ import type { RNG } from './rng' +import type { SeedType } from './types' import { bates } from './distributions/bates' import { bernoulli } from './distributions/bernoulli' import { binomial } from './distributions/binomial' @@ -50,7 +51,7 @@ export class Random { } = {} protected _patch?: typeof Math.random - constructor(rng: RNG = new RNGMathRandom()) { + constructor(rng: SeedType = new RNGMathRandom()) { this.use(rng) } @@ -71,12 +72,8 @@ export class Random { * @param {object} [opts] - Optional config for new RNG options. * @return {Random} */ - clone(...args: [T]): Random { - if (args.length) { - return new Random(RNGFactory(...args)) - } else { - return new Random(this.rng.clone()) - } + clone(rng: SeedType = this.rng.clone()): Random { + return new Random(rng) } /** @@ -97,8 +94,8 @@ export class Random { * * @param {...*} args */ - use(...args: [RNG]) { - this._rng = RNGFactory(...args) + use(rng?: SeedType) { + this._rng = RNGFactory(rng) } /** diff --git a/src/seed.test.ts b/src/seed.test.ts index 850bbb8..71e45fa 100644 --- a/src/seed.test.ts +++ b/src/seed.test.ts @@ -1,7 +1,7 @@ import seedrandom from 'seedrandom' import { expect, test } from 'vitest' -import random from '../src/random' +import random, { Random } from '../src/random' test('random.clone with seedrandom rng is consistent', () => { const r = random.clone(seedrandom('ZjExZDczNWQxY2NlZjUzYmRiZWU0ZGIz')) @@ -26,3 +26,17 @@ test('random.clone with string seed is consistent', () => { expect(o).toMatchSnapshot() }) + +test('Random constructor', () => { + const rng = new Random() + expect(rng).toBeDefined() + + const rng2 = new Random(seedrandom('my-seed-string')) + expect(rng2).toBeDefined() + + const rng3 = new Random(Math.random) + expect(rng3).toBeDefined() + + const rng4 = new Random('example--seed-string') + expect(rng4).toBeDefined() +})