#random #mersenne #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

#2179 in Algorithms

Download history 659/week @ 2024-03-29 177/week @ 2024-04-05 227/week @ 2024-04-12 238/week @ 2024-04-19 278/week @ 2024-04-26 471/week @ 2024-05-03 228/week @ 2024-05-10 220/week @ 2024-05-17 255/week @ 2024-05-24 207/week @ 2024-05-31 440/week @ 2024-06-07 235/week @ 2024-06-14 259/week @ 2024-06-21 119/week @ 2024-06-28 317/week @ 2024-07-05 189/week @ 2024-07-12

938 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

~315–540KB