{"library":"pure-rand","title":"Pure Random Number Generators","description":"Pure-rand is a JavaScript and TypeScript library providing fast, pure pseudorandom number generators (PRNGs). Its core differentiator is the emphasis on purity: generator instances are immutable. This means each random number generation operation returns a new generator state alongside the generated value, enabling reproducible sequences and eliminating side effects, which is crucial for deterministic simulations, testing, and cryptographic applications where state integrity is paramount. The library offers various PRNG algorithms, such as Xoroshiro128+, and implements common statistical distributions like uniform integers. Currently at version 8.4.0, pure-rand maintains an active development and release cadence, providing reliable and performant random number generation. It aims to offer predictable and verifiable randomness, setting it apart from libraries that mutate generator state in place.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pure-rand"],"cli":null},"imports":["import { xoroshiro128plus } from 'pure-rand/generator/xoroshiro128plus';","import { uniformInt } from 'pure-rand/distribution/uniformInt';","import { purify } from 'pure-rand/utils/purify';","import type { IRandomGenerator } from 'pure-rand';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { uniformIntDistribution } from 'pure-rand/distribution/UniformIntDistribution';\nimport { xoroshiro128plus } from 'pure-rand/generator/xoroshiro128plus';\nimport { purify } from 'pure-rand/utils/purify';\n\nconst uniformIntDistributionPure = purify(uniformIntDistribution);\n\n// A seed for reproducibility\nconst seed = 42;\n// Initialize the PRNG. This creates an 'impure' generator.\nconst rng1 = xoroshiro128plus(seed);\n\n// Use the purified distribution. It returns both the value and the *next* generator state.\nconst [firstDiceValue, rng2] = uniformIntDistributionPure(rng1, 1, 6);\nconsole.log(`First roll: ${firstDiceValue}, Next RNG state hash: ${rng2.min()}`); // Example: 2\n\nconst [secondDiceValue, rng3] = uniformIntDistributionPure(rng2, 1, 6);\nconsole.log(`Second roll: ${secondDiceValue}, Next RNG state hash: ${rng3.min()}`); // Example: 4\n\nconst [thirdDiceValue, rng4] = uniformIntDistributionPure(rng3, 1, 6);\nconsole.log(`Third roll: ${thirdDiceValue}, Next RNG state hash: ${rng4.min()}`); // Example: 6\n\n// Demonstrating purity: Calling with rng1 again produces the same result\nconst [replayedFirstValue, _] = uniformIntDistributionPure(rng1, 1, 6);\nconsole.log(`Replayed first roll (from rng1): ${replayedFirstValue}`); // Example: 2\n\n// Example of directly using an impure generator (less common in pure-rand's philosophy)\nconst impureRng = xoroshiro128plus(100);\nconst val1 = uniformInt(impureRng, 1, 10);\nconst val2 = uniformInt(impureRng, 1, 10);\nconsole.log(`Impure sequence: ${val1}, ${val2}`);","lang":"typescript","description":"Demonstrates the library's core 'pure' usage model, where each random number generation returns a new generator state, ensuring immutability and reproducibility. It initializes a Xoroshiro128+ generator, purifies a uniform integer distribution, and shows how to generate a sequence of values while correctly managing the evolving immutable generator state.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}