From 17263f21e750363a8ca8b932100baa431a40420a Mon Sep 17 00:00:00 2001 From: Richard King Date: Sat, 2 Sep 2023 18:00:10 +0200 Subject: [PATCH] feat(library): add SeededGrandom seedable random generator class --- .../src/Grandom/SeededGrandom/index.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/library/src/Grandom/SeededGrandom/index.ts diff --git a/packages/library/src/Grandom/SeededGrandom/index.ts b/packages/library/src/Grandom/SeededGrandom/index.ts new file mode 100644 index 0000000..79795f2 --- /dev/null +++ b/packages/library/src/Grandom/SeededGrandom/index.ts @@ -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['bigint'] + boolean: InstanceType['boolean'] + number: InstanceType['number'] + pick: InstanceType['pick'] + shuffle: InstanceType['shuffle'] + string: InstanceType['string'] + weighted: InstanceType['weighted'] +}