-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(library): add SeededGrandom seedable random generator class
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import MT19937Engine from '@grandom/engines/mt19937' | ||
|
||
import RandomBigInt from '@grandom/bigint/RandomBigInt' | ||
import RandomBoolean from '@grandom/boolean/RandomBoolean' | ||
import RandomNumber from '@grandom/number/RandomNumber' | ||
import RandomPick from '@grandom/pick/RandomPick' | ||
import RandomShuffle from '@grandom/shuffle/RandomShuffle' | ||
import RandomString from '@grandom/string/RandomString' | ||
import RandomWeighted from '@grandom/weighted/RandomWeighted' | ||
|
||
export default class SeededGrandom { | ||
private readonly _engine: MT19937Engine | ||
|
||
constructor () { | ||
this._engine = new MT19937Engine() | ||
|
||
const randomBigInt = new RandomBigInt(this._engine) | ||
const randomBoolean = new RandomBoolean(this._engine) | ||
const randomNumber = new RandomNumber(this._engine) | ||
const randomPick = new RandomPick(this._engine) | ||
const randomShuffle = new RandomShuffle(this._engine) | ||
const randomString = new RandomString(this._engine) | ||
const randomWeighted = new RandomWeighted(this._engine) | ||
|
||
this.bigint = randomBigInt.bigint.bind(randomBigInt) | ||
this.boolean = randomBoolean.boolean.bind(randomBoolean) | ||
this.number = randomNumber.number | ||
this.pick = randomPick.pick.bind(randomPick) | ||
this.shuffle = randomShuffle.shuffle.bind(randomShuffle) | ||
this.string = randomString.string.bind(randomString) | ||
this.weighted = randomWeighted.weighted.bind(randomWeighted) | ||
} | ||
|
||
get seed (): any { | ||
return this._engine.seed | ||
} | ||
|
||
set seed (seed: any) { | ||
this._engine.seed = seed | ||
} | ||
|
||
bigint: InstanceType<typeof RandomBigInt>['bigint'] | ||
boolean: InstanceType<typeof RandomBoolean>['boolean'] | ||
number: InstanceType<typeof RandomNumber>['number'] | ||
pick: InstanceType<typeof RandomPick>['pick'] | ||
shuffle: InstanceType<typeof RandomShuffle>['shuffle'] | ||
string: InstanceType<typeof RandomString>['string'] | ||
weighted: InstanceType<typeof RandomWeighted>['weighted'] | ||
} |