Random.js is a JavaScript library designed to provide mathematically correct and consistent random number generation, addressing the shortcomings of `Math.random()` and common biased implementations. It offers various 'engines' like `nativeMath` (using `Math.random`), `browserCrypto` (using `crypto.getRandomValues`), `nodeCrypto` (using `crypto.randomBytes`), and `MersenneTwister19937` for deterministic, repeatable sequences. The library focuses on providing 32 bits of randomness consistently and includes distributions to prevent common biases in integer generation. The current stable version is 2.1.0, and while a specific release cadence isn't stated, it generally follows semver for major breaking changes. Its key differentiators include platform-specific cryptographic engines and a robust, bias-free API for various distributions, making it suitable for simulations, games, and other applications where high-quality randomness is crucial.
npm install random-jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates importing and initializing a `Random` instance with a `MersenneTwister19937` engine, seeding it for deterministic results, and generating a uniform random integer using the `integer` distribution. It also briefly mentions other engine options.
Rewrite CommonJS `require` statements and adjust object property access to use named `import` statements (e.g., `import { MersenneTwister19937, Random } from 'random-js';`).For deterministic or higher-quality randomness, use random-js engines like `MersenneTwister19937` (seeded) for repeatability, or `nodeCrypto`/`browserCrypto` for cryptographically secure, non-deterministic values where available.
Always use random-js's provided distribution methods, such as `random.integer(min, max)`, which correctly handle bias elimination by potentially drawing multiple times from the underlying engine.
For cryptographically secure random numbers, prefer `browserCrypto` (in browsers) or `nodeCrypto` (in Node.js) engines. Ensure your environment supports these APIs.
If determinism or a long period is required, use `MersenneTwister19937`. For cryptographically secure numbers, use `browserCrypto` or `nodeCrypto`. Only use `nativeMath` when simple, non-critical, non-deterministic randomness is acceptable.
Ensure `MersenneTwister19937` is imported via named ES module syntax and that you call `.seed()` on it to get an engine instance: `import { MersenneTwister19937 } from 'random-js'; const engine = MersenneTwister19937.seed(123);`Make sure to import `Random` as a named export (`import { Random } from 'random-js';`) and instantiate it with a valid engine (`new Random(MersenneTwister19937.seed(123))`).Use the appropriate engine for your environment (`nodeCrypto` in Node.js, `browserCrypto` in modern browsers). For cross-platform or older browser compatibility, `MersenneTwister19937` is a better choice.
No dependency data recorded yet.