Crate xorshift [−] [src]
Implementation of the high performance xoroshiro128+, xorshift128+, xorshift1024*, and splitmix64 pseudo random number generators.
Implements the Rand
, Rng
, and SeedableRng
traits from the rand crate.
Usage
[dependencies]
xorshift = "0.1"
extern crate xorshift;
Examples
extern crate time; extern crate xorshift; use time::precise_time_ns; use xorshift::{Rand, Rng, SeedableRng, SplitMix64, Xoroshiro128, Xorshift128, Xorshift1024}; fn main() { // Use the high-resolution performance counter for seeding let now = precise_time_ns(); // Manually seed a Xorshift128+ PRNG let states = [now, now]; let mut rng: Xorshift128 = SeedableRng::from_seed(&states[..]); println!("Xorshift128+ random u64: {}", rng.next_u64()); // Use a SplitMix64 PRNG to seed a Xoroshiro128+ PRNG let mut sm: SplitMix64 = SeedableRng::from_seed(now); let mut rng: Xoroshiro128 = Rand::rand(&mut sm); println!("Xoroshiro128+ random u64: {}", rng.next_u64()); let mut rng: Xorshift1024 = Rand::rand(&mut sm); println!("Xorshift1024* random u64: {}", rng.next_u64()); // Generate 20 random u32s let vals = rng.gen_iter::<u32>().take(20).collect::<Vec<u32>>(); println!("Xorshift1024* random u32: {:?}", vals); // Generate 50 random u64s let vals = rng.gen_iter::<u64>().take(50).collect::<Vec<u64>>(); println!("Xorshift1024* random u64: {:?}", vals); }
Parallelism
Applications with little parallelism, should use the Xoroshiro128+ generator.
For large scale parallel computations, use Xorshift1024*. Either use the
thread_rng()
function to create generators with the same seed but incremented
jump states or explicitly use the jump function to forward generator
state.
extern crate xorshift; use std::thread; use xorshift::{Rng, Xorshift1024}; use xorshift::thread_rng; fn main() { let mut threads = Vec::new(); for i in 0..17 { threads.push(thread::spawn(move || { let mut r: Xorshift1024 = thread_rng(); println!("Thread: {}, random u64: {}", i, r.next_u64()); })); } for child in threads { let _ = child.join(); } }
extern crate time; extern crate xorshift; use std::thread; use time::precise_time_ns; use xorshift::{Rand, Rng, RngJump, SeedableRng, SplitMix64, Xorshift1024}; fn main() { // Use the high-resolution performance counter for seeding let now = precise_time_ns(); let mut sm: SplitMix64 = SeedableRng::from_seed(now); let rng: Xorshift1024 = Rand::rand(&mut sm); let mut threads = Vec::new(); for i in 0..17 { threads.push(thread::spawn(move || { let mut r = rng; r.jump(i); println!("Thread: {}, random u64: {}", i, r.next_u64()); })); } for child in threads { let _ = child.join(); } }
Reexports
pub use splitmix64::SplitMix64; |
pub use xoroshiro128::Xoroshiro128; |
pub use xorshift128::Xorshift128; |
pub use xorshift1024::Xorshift1024; |
Modules
splitmix64 |
The |
xoroshiro128 |
The Xoroshiro128+ random number generator. |
xorshift128 |
The Xorshift128+ random number generator. |
xorshift1024 |
The Xorshift1024* random number generator. |
Structs
StdRng |
The standard RNG. This is designed to be efficient on the current platform. |
Traits
Rand |
A type that can be randomly generated using an |
Rng |
A random number generator. |
RngJump |
A random number generator with jumpable state. |
SeedableRng |
A random number generator that can be explicitly seeded to produce the same stream of randomness multiple times. |
Functions
thread_rng |
Create a jumpable random number generator. Each call increments the generator jump state. |