
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
random-seedable
Advanced tools
Fully-fledged collection of both 32 and 64 bit seedable random number generators whose output precisely matches their original c/c++ implementations.
Fully-fledged random number generator library offering both 32 bit and 64 bit high quality implementations of Xorshift, Xorwow, Mersenne Twister, PCG, and LCG. Each implements a standard API producing number distributions that exactly match the original implementations. Typescript compatible.
npm install random-seedable --save
Just want to use a random generator with no hassle? All you've got to do is import random, and use it as you would a generator you initialised yourself.
Simply import random and call whatever method you please,
import random from 'random-seedable';
random.bool(); // false
random.int(); // 2346
random.float(); // 0.34527
random.shuffle([1, 2, 3, 1, 4]); // [2, 4, 3, 1, 1]
random.choice([1, 2, 3, 1, 4]); // 2
Want a random generator with a seed you can set? Just import the generator you wish and initialise it yourself.
import { XORShift } from 'random-seedable';
const random = new XORShift(123456789);
random.int(); // 123312
random.bool(); // true
random.shuffle([1, 2, 3, 1, 4]); // [2, 4, 3, 1, 1]
random.choice([1, 2, 3, 1, 4]); // 2
Once the generator is initialised, the seed can be changed using the seed setter function.
import { XORShift } from 'random-seedable';
const random = new XORShift(123456789);
console.log(random.seed); // 123456789
random.seed = 987654321;
console.log(random.seed); // 987654321
Supported PRNGs and their default initialisations.
| Class | Default Initialisation | Integer output |
|---|---|---|
| LCG | new LCG(Date.now(), 1664525, 1013904223, 4294967296); | 32 bit |
| PCG | new PCG(Date.now(), 6364136223846793005n, 1442695040888963407n); | 32 bit |
| MersenneTwister | new MersenneTwister(Date.now(), 624, 397); | 32 bit |
| XORShift | new XORShift(Date.now(), 13, 17, 5); | 32 bit |
| XORShift64 | new XORShift64(Date.now(), 13, 7, 17); | 64 bit |
| XORShift128 | new XORShift128(Date.now(), 362436069, 521288629, 88675123); | 32 bit |
| XORShift128Plus | new XORShift128Plus(Date.now(), 362436069); | 64 bit |
| XORWow | new XORWow(Date.now(), 362436069, 521288629, 88675123, 5783321, 6615241, 362437); | 32 bit |
| random | default PRNG, same as XORShift64 | 64 bit |
Each PRNG has the following methods.
| Method | Parameters | Return |
|---|---|---|
| .bool() | None | Boolean. |
| .coin(pTrue) | pTrue:Number | Boolean. |
| .int() | None | Number. |
| .bigInt() | None | BigInt. |
| .float() | None | Float. |
| .float53() | None | Float spread over full range. |
| .randRange(min, max) | min:Number, max:Number | min <= Number <= max |
| .randBelow(max) | max:Number | Number <= Max |
| .choice(array) | array:[?] | Item from array of type ? |
| .shuffle(array, inPlace = true) | array:[?], inPlace:Boolean | Shuffled Array[?] |
| .boolArray(size) | size:Number | Array[Boolean] of length size. |
| .coinArray(size, pTrue) | size:Number, pTrue:Number | Array[Boolean] of length size. |
| .intArray(size) | size:Number | Array[Number] of length size. |
| .bigIntArray(size) | size:Number | Array[BigInt] of length size. |
| .randRangeArray(size, min, max) | size:Number, min:Number, max:Number | Array[Number] of length size filled w/ min <= num <= max. |
| .floatArray(size) | size:Number | Array[Number] between 0.0 - 1.0 of length size. |
| .float53Array(size) | size:Number | Array[Number] between 0.0 - 1.0 of length size. |
Linear Congruential Generator (LCG) is a simple generator originally devised in 1951, if you need something quick with minimal memory usage and not the best quality randomness, this is for you. 32 bits of output.
const random = new LCG(1234, 1664525, 1013904223, 4294967296);
Permuted Congruential Generator (PCG) is again, a relatively simple generator that improves on the qualites of LCG by improving its randomness quality by increasing its state size and using only the most significant bits to produce the output. 32 bits of output.
const random = new PCG(0x4d595df4d0f33173n, 6364136223846793005n, 1442695040888963407n);
Mersenne Twister is a widely used PRNG, most well known for being the Python and Excel default with an extremely large state. 32 bits of output.
const random = new MersenneTwister(5489, 624, 397);
XorShift generators are fast, efficient generators with good randomness quality. This generator has 32 bit output with 32 bits of internal state.
const random = new XORShift(11234, 13, 17, 5);
XorShift generators are fast, efficient generators with good randomness quality. This implementation has 64 bit output with 64 bits of internal state.
const random = new XORShift64(11234, 13, 7, 17);
XorShift generators are fast, efficient generators with good randomness quality. This implementation has 32 bit output with 128 bits of internal state.
const random = new XORShift128(Date.now(), 362436069, 521288629, 88675123);
XorShift generators are fast, efficient generators with good randomness quality. 64 bits of output with 128 internal state.
const random = new XORShift128Plus(Date.now(), 362436069);
XorWow is an improved version of XorShift and default generator of Nvidia CUDA. 32 bits of output.
const random = new XORWow(123456789, 362436069, 521288629, 88675123, 5783321, 6615241, 362437);
random.bool()
Generates a boolean with the formula random.float() >= 0.5
None.
Boolean True/False.
random.bool(); // true
random.coin(pTrue)
Generates a random boolean with probability of it being true denoted by the pTrue parameter. For example, when pTrue=0.8, 80% of the numbers generated with this method will be true.
Boolean True/False.
random.coin(0.8); // true
random.int()
Generates and returns the next number in the PRNGs sequence.
None.
Number less than 2 ** 32 for 32 bit generators.
random.int(); // 85424123
random.bigInt()
Generates and returns the next number in the PRNGs sequence and returns it as a Bigint.
None.
Number less than 2 ** 32 for 32 bit generators represented as a BigInt class. Further reading on Big Integers
random.bigInt(); // 85424123n
random.float()
Generates a random floating point number.
None.
Float between 0.0 - 1.0.
random.float(); // 0.234242
random.float53()
Generates a random floating point number.
None.
Float between 0.0 - 1.0.
random.float53(); // 0.2342422341231
random.randRange(min, max)
Generates a number within the given range.
Number min <= Number <= max.
const lowerBound = 4;
const upperBound = 2432;
random.randRange(lowerBound, upperBound); // 36.
random.randBelow(max)
Generates a number below the given maximum.
Number <= max
const upperBound = 2432;
random.randBelow(upperBound); // 285.
random.choice(array)
Picks a random element from the array.
A singular item from the array of type ?.
const arr = [1, 4, 2, 3];
random.choice(arr); // 4
random.shuffle(array, inPlace = false)
Randomly shuffles the given array using the fisher-yates algorithm.
Array shuffled (inPlace === false), shuffled copy of array (inPlace === true).
const arr = [1, 4, 2, 3];
const shuffled = random.shuffle(arr, false);
console.log(arr); // [1, 4, 2, 3]
console.log(shuffled); // [4, 2, 3, 1]
const arr = [1, 4, 2, 3];
const shuffled = random.shuffle(arr, true);
console.log(arr); // [4, 2, 3, 1]
console.log(shuffled); // [4, 2, 3, 1]
random.boolArray(size)
Generates an n size array populated with booleans.
Array[Boolean] of length size.
const size = 256;
random.boolArray(size);
random.coinArray(size, pTrue)
Generates an n size array of random booleans with probability of it being true denoted by the pTrue parameter. For example, when pTrue=0.8, 80% of the numbers in the generated array will be true.
Array[Boolean] of length size.
const size = 256;
const pTrue = 0.8;
random.coinArray(size, pTrue);
random.intArray(size)
Generates an n size array populated with integers.
Array[Number] of length size.
const size = 256;
random.intArray(size);
random.bigIntArray(size)
Generates an n size array populated with Big Integers.
Array[BigInt] of length size.
const size = 256;
random.bigIntArray(size);
random.randRangeArray(size, min, max)
Generates an n size array populated within the given range.
Array[Number] of length size filled w/ min <= num <= max.
const size = 256;
const lowerBound = 4;
const upperBound = 2432;
random.randRangeArray(size, lowerBound, upperBound);
random.floatArray(size)
Generates an n size array populated with floats.
Array[Number] between 0.0 - 1.0 of length size.
const size = 256;
random.floatArray(size);
random.float53Array(size)
Generates an n size array populated with floats.
Array[Number] between 0.0 - 1.0 of length size.
const size = 256;
random.float53Array(size);
See LICENSE file.
FAQs
Fully-fledged collection of both 32 and 64 bit seedable random number generators whose output precisely matches their original c/c++ implementations.
The npm package random-seedable receives a total of 8,506 weekly downloads. As such, random-seedable popularity was classified as popular.
We found that random-seedable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.