#random #hash #wyhash #performance

no-std wyrand

A fast & portable non-cryptographic pseudorandom number generator and hashing algorithm

9 releases

0.2.1 Jul 22, 2024
0.2.0 Apr 18, 2024
0.1.6 Mar 26, 2024
0.1.3 Feb 28, 2024
0.1.1 Mar 25, 2023

#194 in Algorithms

Download history 285/week @ 2024-07-22 244/week @ 2024-07-29 142/week @ 2024-08-05 212/week @ 2024-08-12 144/week @ 2024-08-19 178/week @ 2024-08-26 153/week @ 2024-09-02 138/week @ 2024-09-09 183/week @ 2024-09-16 154/week @ 2024-09-23 195/week @ 2024-09-30 43/week @ 2024-10-07 209/week @ 2024-10-14 150/week @ 2024-10-21 168/week @ 2024-10-28 126/week @ 2024-11-04

665 downloads per month
Used in 6 crates (3 directly)

Apache-2.0 OR MIT

62KB
1.5K SLoC

WyRand-rs

CI License Cargo Documentation

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::WyRand;

// Provide a seed to the PRNG
let mut rng = WyRand::new(Default::default());

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 for rand_core, implementing RngCore & SeedableRng on WyRand.
  • debug - Enables core::fmt::Debug implementation for WyRand/WyHash.
  • serde1 - Enables Serialize and Deserialize derives on WyRand.
  • hash - Enables core::hash::Hash implementation for WyRand.
  • wyhash - Enables WyHash, a fast & portable hashing algorithm. Based on the final v4 C implementation.
  • randomised_wyhash - Enables RandomWyHashState, a means to source a randomised state for WyHash for use in collections like HashMap/HashSet. Enables wyhash feature if it is not already enabled.
  • fully_randomised_wyhash - Randomises not just the seed for RandomWyHashState, but also the secret. The new secret is generated once per runtime, and then is used for every subsequent new WyHash (with each WyHash instance having its own unique seed). Enables randomised_wyhash if not already enabled, and requires std environments.
  • threadrng_wyhash - Enables sourcing entropy from rand's thread_rng() method. Much quicker than getrandom. Enables randomised_wyhash if not already enabled. Requires std environments.
  • 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:

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.2", features = ["js"] }

License

Licensed under either of

at your option.

Dependencies

~35–400KB