Skip to content

Commit

Permalink
feat(library): add SeededGrandom seedable random generator class
Browse files Browse the repository at this point in the history
  • Loading branch information
richrdkng committed Sep 2, 2023
1 parent ad65967 commit 17263f2
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/library/src/Grandom/SeededGrandom/index.ts
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']
}

0 comments on commit 17263f2

Please sign in to comment.