PitchHut
Log in / Sign up
mtwist
6 views
Reliable randomness for your JavaScript applications.
Pitch

Discover a concise Mersenne Twister implementation for JavaScript that ensures reproducible pseudo-random sequences. With enhancements and TypeScript compatibility, this library reflects the latest in random number generation while maintaining widespread usage across popular programming languages. Streamline your projects with consistent, evenly distributed random values.

Description

Introducing mtwist: a concise yet powerful implementation of the Mersenne Twister Pseudo-Random Number Generator (PRNG) designed specifically for JavaScript and CoffeeScript users. This library delivers reliable randomness that is simple to implement and is rigorously tested against the original C implementation, ensuring compatibility and performance.

Features of mtwist:

  • Initialization Enhancements: Incorporates improvements made to the Mersenne Twister algorithm in 2022, enhancing its efficiency and reliability. Learn more about these improvements here.
  • TypeScript Support: The library comes with TypeScript types, making it easier to integrate into TypeScript projects while providing enhanced code safety and autocompletion.

Why Choose Mersenne Twister?

The Mersenne Twister is a well-established PRNG, widely adopted as the default generator for numerous programming environments, including Excel, R, Python, and Ruby. While it may not be the most modern or cryptographically secure option, it is ideal for specific use cases:

  • Reproducibility: By manually seeding the PRNG, you can generate reproducible pseudo-random sequences. This is crucial in simulations, testing, or any application requiring consistency.
  • Cross-Browser Compatibility: If your project demands the same random number outputs across all web browsers and JavaScript engines, mtwist is the solution you need.

Quick Usage Example:

Using mtwist is straightforward. Replace the standard random number generation with the Mersenne Twister implementation:

import MersenneTwister from 'mtwist';

// Create a new Mersenne Twister instance with a specific seed
const seed = 1234567890;  // Must be between 0 and 4294967295
const m = new MersenneTwister(seed);

// Generate a random number
const x = m.random();

In addition to generating floating-point numbers, mtwist provides methods for producing evenly distributed integers within specified ranges, adding flexibility to your random number generation needs.

For additional details on usage and function references, visit index.ts.

Embrace the power of reliable random number generation with mtwist in your next JavaScript or CoffeeScript project!