Random number generators
Random number generators need to provide next_u32 and next_u64 methods, other methods can be mixed in.
Types implementing Rng should also mix in RngExt.
Example implementation
use std::random::{Rng, RngExt};
struct Lcg {
state: u32,
}
impl Lcg {
fn with_seed(seed: u32) -> Lcg {
Lcg { state: seed }
}
fn next_u32(self: &mut Lcg) -> u32 {
self.state = 1664525u32 * self.state + 1013904223u32;
self.state
}
fn next_u64(self: &mut Lcg) -> u64 {
(self.next_u32() as u64) << 32 | (self.next_u32() as u64)
}
mixin Rng<Lcg>;
mixin RngExt<Lcg>;
}
let rng = Lcg::with_seed(0x15b3249);
for _ in 0..10 {
println!("{}", rng.next(0..10));
}
Run this example
Required methods
-
Generate a 32-bit integer.
-
Generate a 64-bit integer.
Provided methods
-
Fill the slice with random bytes.