#rng

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

#37 in #rng

Download history 480/week @ 2024-07-19 281/week @ 2024-07-26 177/week @ 2024-08-02 221/week @ 2024-08-09 174/week @ 2024-08-16 179/week @ 2024-08-23 161/week @ 2024-08-30 162/week @ 2024-09-06 161/week @ 2024-09-13 190/week @ 2024-09-20 133/week @ 2024-09-27 68/week @ 2024-10-04 135/week @ 2024-10-11 119/week @ 2024-10-18 128/week @ 2024-10-25 173/week @ 2024-11-01

557 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

~330–560KB