#random #numbers #randomness #api #thread-local

srand

Random number generators and other randomness functionality with simple apis to use

7 unstable releases (3 breaking)

0.4.0 Sep 15, 2019
0.3.2 Sep 14, 2019
0.2.1 Sep 12, 2019
0.1.0 Sep 11, 2019

#1641 in Algorithms

31 downloads per month

Apache-2.0

37KB
1K SLoC

Srand

Random number generators and other randomness functionality inspired by golang standard library with simple apis to use.

A simple rng random generator

let mut r: Rand<_> = Rand::new(RngSource::new(1));
let mut get = vec![];
for _i in 0..50 {
    get.push(r.int32n(100));
}

A threads safe random generator

let r: Rand<_> = Rand::new(LockedSource::new(1));
let mut handles = vec![];
for i in 0..4 {
    let mut r = r.clone();
    let h = std::thread::spawn(move || {
        for j in 0..3 {
            println!("thread: {}, index: {}, {}", i, j, r.i64());
        }
    });
    handles.push(h);
}
for h in handles {
    h.join().unwrap();
}

Thread local apis

srand::ThreadLocal::seed(1234567);
srand::ThreadLocal::int32();
srand::ThreadLocal::uint32();
srand::ThreadLocal::int64();
srand::ThreadLocal::uint64();

Random data

let mut buffer = Vec::with_capacity(16);
buffer.resize(16, 0u8);
srand::read(&mut buffer);

Dependencies