#random #mersenne #rand-rng #rng #rand #algorithm

mersenne_twister

Mersenne Twister pseudorandom number generator

11 releases (3 stable)

Uses old Rust 2015

1.1.1 Jan 17, 2018
1.1.0 Feb 23, 2017
1.0.0 Jan 20, 2017
0.3.0 May 30, 2015
0.1.0 Feb 2, 2015

#2159 in Algorithms

Download history 233/week @ 2023-11-24 123/week @ 2023-12-01 178/week @ 2023-12-08 246/week @ 2023-12-15 245/week @ 2023-12-22 246/week @ 2023-12-29 186/week @ 2024-01-05 248/week @ 2024-01-12 155/week @ 2024-01-19 141/week @ 2024-01-26 127/week @ 2024-02-02 187/week @ 2024-02-09 286/week @ 2024-02-16 286/week @ 2024-02-23 187/week @ 2024-03-01 158/week @ 2024-03-08

927 downloads per month
Used in 256 crates (3 directly)

MIT/Apache

89KB
1.5K SLoC

Mersenne Twister in Rust

This is a pure rust port of the Mersenne Twister pseudorandom number generators. See the rustdoc for suggested usage.

Algorithms

  • MT19937 (32-bit version)
  • MT19937-64 (64-bit version)

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Mersenne Twister

A pure rust port of the Mersenne Twister pseudorandom number generator.

THESE ALGORITHMS ARE NOT APPROPRIATE FOR CRYPTOGRAPHIC USE. After observing a couple hundred outputs, it is possible to predict all future outputs. This library even implements a recover constructor to reconstruct the RNG state from output samples.

Usage

If your application does not require a specific Mersenne Twister flavor (32-bit or 64-bit), you can use the default flavor for your target platform by using the MersenneTwister type definition. Either flavor accepts a u64 seed.

extern crate mersenne_twister;
extern crate rand;
use mersenne_twister::MersenneTwister;
use rand::{Rng, SeedableRng};

fn main() {
    // Get a seed somehow.
    let seed: u64 = 0x123456789abcdef;
    // Create the default RNG.
    let mut rng: MersenneTwister = SeedableRng::from_seed(seed);

    // start grabbing randomness from rng...
}

Or if you want to use the default (fixed) seeds that are specified in the reference implementations:

use std::default::Default;
let mut rng: MersenneTwister = Default::default();

Portability

Note that MT19937 and MT19937_64 are not identical algorithms, despite their similar names. They produce different output streams from the same seed. You will need to pick a specific flavor of the two algorithms if portable reproducibility is important to you.

Dependencies

~320–550KB