0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-24 00:17:37 +01:00
mongodb/jstests/libs/lcg_random.js
Kevin Cherkauer 34831783f1 SERVER-89033 Create plan cache targeted fuzzer (#22590)
GitOrigin-RevId: 022c2fcb84e9a3300162d5da67b78c98e56b9e0e
2024-09-04 23:10:39 +00:00

32 lines
1.2 KiB
JavaScript

/**
* An implementation of the Lehmer / Parker-Miller linear congruential generator, a fast
* pseudorandom number generator that can be seeded and of which multiple instances can be created.
* (JavaScript's native random number generator cannot be seeded and is shared among everything
* generating random numbers.)
*/
export class LcgRandom {
// Constructs an instance with a given seed. Any integer can be input as the seed, however for
// correct operation the seed used to initialize the state must be in the range [1, 2147483646],
// so if the input seed is outside that range it is coerced into it.
constructor(seed) {
seed = seed % 2147483647;
if (seed < 0) {
seed *= -1;
} else if (seed == 0) {
seed = 1;
}
this._seed = seed;
}
// Returns an integer value in the range [min, max).
getRandomInt(min, max) {
return Math.floor(this.getRandomFloat() * (max - min)) + min;
}
// Returns a floating point value in the range [0, 1).
getRandomFloat() {
this._seed = this._seed * 48271 % 2147483647;
return (this._seed - 1) / 2147483646;
}
} // class LcgRandom