Skip to content

Commit

Permalink
🎵
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Sep 4, 2024
1 parent 43332e9 commit 1e86b74
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export * from './generators'
export * from './random'
export { default } from './random'
export * from './rng'
export * from './rng-factory'
export type * from './types'
export * from './utils'
7 changes: 4 additions & 3 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { uniform } from './distributions/uniform'
import { uniformBoolean } from './distributions/uniform-boolean'
import { uniformInt } from './distributions/uniform-int'
import { MathRandomRNG } from './generators/math-random'
import { RNGFactory } from './rng-factory'
import { createRNG } from './utils'

/**
* Distribution function
Expand Down Expand Up @@ -47,8 +47,9 @@ export class Random {
protected readonly _cache: {
[k: string]: ICacheEntry<any>
} = {}

constructor(seedOrRNG: SeedOrRNG = new MathRandomRNG()) {
this._rng = RNGFactory(seedOrRNG)
this._rng = createRNG(seedOrRNG)
}

/**
Expand Down Expand Up @@ -81,7 +82,7 @@ export class Random {
* ```
*/
use(seedOrRNG: SeedOrRNG) {
this._rng = RNGFactory(seedOrRNG)
this._rng = createRNG(seedOrRNG)
}

// --------------------------------------------------------------------------
Expand Down
22 changes: 0 additions & 22 deletions src/rng-factory.ts

This file was deleted.

23 changes: 22 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
import type { Seed } from './types'
import type { Seed, SeedOrRNG } from './types'
import { ARC4RNG } from './generators/arc4'
import { FunctionRNG } from './generators/function'
import { RNG } from './rng'

export function createRNG(seedOrRNG?: SeedOrRNG) {
switch (typeof seedOrRNG) {
case 'object':
if (seedOrRNG instanceof RNG) {
return seedOrRNG
}
break

case 'function':
return new FunctionRNG(seedOrRNG)

default:
return new ARC4RNG(seedOrRNG)
}

throw new Error(`invalid RNG seed or instance "${seedOrRNG}"`)
}

export function processSeed(seed?: Seed): number {
if (seed === undefined) {
Expand Down

0 comments on commit 1e86b74

Please sign in to comment.