What is pure-rand?
The pure-rand npm package is a library designed for generating random numbers in a pure and functional way. It supports various types of random number generation methods and can be used in scenarios where deterministic results are needed from random inputs by providing seed-based generation.
What are pure-rand's main functionalities?
Generating random integers
This feature allows the generation of random integers within a specified range. The example shows how to generate a random integer between 1 and 100 using a seeded Mersenne Twister algorithm.
import { Random, MersenneTwister19937 } from 'pure-rand';
const randomGenerator = MersenneTwister19937.seed(1234);
const randomInt = Random.integer(1, 100)(randomGenerator);
console.log(randomInt);
Generating random arrays
This feature enables the creation of arrays filled with random integers. The code sample demonstrates generating an array of 10 random integers, each between 1 and 100.
import { Random, MersenneTwister19937 } from 'pure-rand';
const randomGenerator = MersenneTwister19937.seed(5678);
const randomArray = Random.array(Random.integer(1, 100), 10)(randomGenerator);
console.log(randomArray);
Other packages similar to pure-rand
random-js
random-js is a mathematically correct random number generator library for JavaScript. It offers a similar range of features as pure-rand, including seed-based generation, but with additional utilities for generating random values of more complex types and distributions.
seedrandom
seedrandom is another popular random number generation library that provides seeded random number generators. It is simpler and smaller than pure-rand but lacks some of the advanced features like generating random arrays or using different algorithms.