Skip to content

Commit

Permalink
fix: example types
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Sep 3, 2024
1 parent 68f0c19 commit 2d2f142
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/distributions/normal.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -15,7 +16,7 @@ test('random.normal() produces numbers', () => {
}
})

const meanN = <T>(t: T) => {
const meanN = <T extends SeedType>(t: T) => {
const r = random.clone(t)
const d = r.normal(120)
let sum = 0
Expand Down
15 changes: 6 additions & 9 deletions src/random.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
}

Expand All @@ -71,12 +72,8 @@ export class Random {
* @param {object} [opts] - Optional config for new RNG options.
* @return {Random}
*/
clone<T>(...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)
}

/**
Expand All @@ -97,8 +94,8 @@ export class Random {
*
* @param {...*} args
*/
use(...args: [RNG]) {
this._rng = RNGFactory(...args)
use(rng?: SeedType) {
this._rng = RNGFactory(rng)
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/seed.test.ts
Original file line number Diff line number Diff line change
@@ -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'))
Expand All @@ -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()
})

0 comments on commit 2d2f142

Please sign in to comment.