#random #pseudo-random #generator #xorwow

xorwowgen

Several implementations of the Xorwow algorithm

4 releases (breaking)

0.4.0 Sep 8, 2024
0.3.0 Aug 19, 2024
0.2.0 Aug 17, 2024
0.1.0 Aug 15, 2024

#1794 in Algorithms

Download history 89/week @ 2024-08-10 286/week @ 2024-08-17 10/week @ 2024-08-24 141/week @ 2024-09-07 19/week @ 2024-09-14 34/week @ 2024-09-21

194 downloads per month

MIT license

20KB
298 lines

Xorwow Generators

Several implementations of the Xorwow generator as proposed here:

https://www.jstatsoft.org/article/view/v008i14

Implements the SeedableRng and RngCore traits from rand_core, so the crate may be used to create various kinds of pseudorandom number sequences.

Examples

Get a few pseudorandom u32 integers:

use rand_core::{SeedableRng, RngCore}; // version = "0.6.4"
use xorwowgen::Xorwow160;

fn main() {
    // initialize the pseudorandom number generator
    let mut rng = Xorwow160::seed_from_u64(123456789);

    // clock it a few times
    for _ in 0..100 {
        rng.next_u32();
    }

    // generate a few numbers
    for _ in 0..10 {
        println!("{}", rng.next_u32());
    }
}

Shuffle mutable array:

use xorwowgen::xorwow64::WrapA;
use rand::SeedableRng; // version = "0.8.5"
use rand::seq::SliceRandom;

fn main() {
    // initialize with true random bytes
    let mut rng = WrapA::from_entropy();

    let mut my_data = ["foo", "bar", "baz", "qux"];

    // shuffle data and print the result
    my_data.shuffle(&mut rng);
    println!("{:?}", &my_data);

    // shuffle again ...
    my_data.shuffle(&mut rng);
    println!("{:?}", &my_data);
}

Notes

  • The generators in this crate are not suitable for any kind of cryptographical use.

  • If you need more functionality than just generating u32 or u64 integers, I highly recommend to have a look at the rand book. As mentioned above, SeedableRng and RngCore are implemented.

Dependencies

~87–425KB