Crate rand_mt

source ·
Expand description

Mersenne Twister random number generators.

This is a native Rust implementation of a selection of Mersenne Twister generators. Mersenne Twister is not suitable for cryptographic use.

This crate provides:

  • Mt19937GenRand32, the original reference Mersenne Twister implementation known as MT19937. This is a good choice on both 32-bit and 64-bit CPUs (for 32-bit output).
  • Mt19937GenRand64, the 64-bit variant of MT19937 known as MT19937-64. This algorithm produces a different output stream than MT19937 and produces 64-bit output. This is a good choice on 64-bit CPUs.

Both of these RNGs use approximately 2.5 kilobytes of state. Mt19937GenRand32 uses a 32-bit seed. Mt19937GenRand64 uses a 64-bit seed. Both can be seeded from an iterator of seeds.

Both RNGs implement a recover constructor which can reconstruct the RNG state from a sequence of output samples.

Usage

You can seed a RNG and begin sampling it:

// Create the RNG.
let mut rng = Mt64::new(0x1234_567_89ab_cdef_u64);
// start grabbing randomness from rng...
let mut buf = vec![0; 512];
rng.fill_bytes(&mut buf);

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

let default = Mt::default();
let mt = Mt::new_unseeded();
assert_eq!(default, mt);

Crate Features

rand_mt is no_std compatible. rand_mt has several optional features that are enabled by default:

  • rand-traits - Enables a dependency on rand_core. Activating this feature implements RngCore and SeedableRng on the RNGs in this crate.
  • std - Enables a dependency on the Rust Standard Library. Activating this feature enables std::error::Error impls on error types in this crate.

Mersenne Twister requires approximately 2.5 kilobytes of internal state. To make the RNGs implemented in this crate practical to embed in other structs, you may wish to store the RNG in a Box.

Structs

  • The 32-bit flavor of the Mersenne Twister pseudorandom number generator.
  • The 64-bit flavor of the Mersenne Twister pseudorandom number generator.

Enums

  • Error returned from fallible Mersenne Twister recovery constructors.

Type Definitions