WyRand-rs
A fast & portable non-cryptographic pseudorandom number generator written in Rust, and optionally, the hashing algorithm as well.
The implementations for both the PRNG and hasher are based on the C reference implementation wyhash, a simple and fast hasher but not cryptographically secure. It's known to be extremely fast and performant while still having great statistical properties.
This crate provides both the v4.2 final implementation of the WyRand/WyHash algorithm, or the older final v4 implementation. The two versions have different outputs due to changes in the algorithm and also with the constants used. By default, the final v4.2 algorithm will be used. If one needs to use the older, legacy v4 implementation for compatibility/stability reasons, the legacy hasher and PRNG can be exposed by enabling the legacy_v4 feature flag.
This crate can be used on its own or be integrated with rand_core/rand, and it is no-std compatible. Minimum compatible Rust version is 1.70. This crate is also implemented with no unsafe code via #![forbid(unsafe_code)].
Example
Generate a random value:
use WyRand;
// Provide a seed to the PRNG
let mut rng = new;
let value = rng.rand;
Features
The crate will always export WyRand and will do so when set as default-features = false in the Cargo.toml. By default, it will have the rand_core, debug features enabled.
rand_core- Enables support forrand_core, implementingRngCore&SeedableRngonWyRand.debug- Enablescore::fmt::Debugimplementation forWyRand/WyHash.serde1- EnablesSerializeandDeserializederives onWyRand.hash- Enablescore::hash::Hashimplementation forWyRand.wyhash- EnablesWyHash, a fast & portable hashing algorithm. Based on the final v4 C implementation.randomised_wyhash- EnablesRandomWyHashState, a means to source a randomised state forWyHashfor use in collections likeHashMap/HashSet. Enableswyhashfeature if it is not already enabled.fully_randomised_wyhash- Randomises not just the seed forRandomWyHashState, but also the secret. The new secret is generated once per runtime, and then is used for every subsequent newWyHash(with eachWyHashinstance having its own unique seed). Enablesrandomised_wyhashif not already enabled, and requiresstdenvironments.threadrng_wyhash- Enables sourcing entropy fromrand'sthread_rng()method. Much quicker thangetrandom. Enablesrandomised_wyhashif not already enabled. Requiresstdenvironments.legacy_v4- Exposes the legacy PRNG/Hashing algorithms that use the final v4 implementation.
Building for WASM/Web
If you are using WyRand with rand_core and/or WyHash with randomised_wyhash then for building for the web/WASM, you'll need to configure getrandom to make use of the browser APIs in order to source entropy from. Add the following to your project Cargo.toml if your WASM builds target the web:
[]
= { = "0.2", = ["js"] }
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.