Drand
Generates random number for Nodejs (and browser) using Mersenne Twister.
Usage
Real Numbers
Function | Description | Bounds |
---|---|---|
r.rand() |
Returns a continuous random number between 0 and 1 | [0, 1) |
r.rand(ub) |
Returns a continuous random number bounded by 0 and the provided upperbound | [0, upperBound) |
r.rand(lb, ub) |
Returns a continuous random number bounded by the parameters | [lowerBound, upperBound) |
Integers - 32 bit (31 + 1 sign bit)
The following functions use a 32 bit sign safe 32 bit mask.
Function | Description | Bounds |
---|---|---|
r.randInt() |
Returns a random integer between 0 and 1 | [-2^32, 2^32-1) |
r.randInt(ub) |
Returns a random integer bounded by 0 and the provided upperbound | [0, upperBound) |
r.randInt(lb, ub) |
Returns a random integer bounded by the parameters | [lowerBound, upperBound) |
Longs - 54 bit (53 + 1 sign bit)
Function | Description | Bounds |
---|---|---|
r.randLong() |
Returns a random integer between 0 and 1 | [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER) |
r.randLong(ub) |
Returns a random integer bounded by 0 and the provided upperbound | [0, upperBound) |
r.randLong(lb, ub) |
Returns a random integer bounded by the parameters | [lowerBound, upperBound) |
Math
Global -> All of the functions described above are available from the global Math
context. The functions are
added by default when loading the library. Note that the static instance is seeded with a random
key. You can access them using
MathMathMath
You can also seed the static instance using Drand.setGlobal(seed)
. This can be done at any time
and as many times as desired. For example
const seed = 1234Drandconst rand1 = MathDrandconst rand2 = Math
See the demo on Runkit for more.