Expand description
Fast random number generators.
The implementations use Wyrand, a simple and fast generator but not cryptographically secure, and ChaCha8, a cryptographically secure generator tuned to 8 rounds of the ChaCha algorithm in order to increase throughput considerably without sacrificing too much security, as per the recommendations set out in the Too Much Crypto paper.
§Examples
Generate a random value:
ⓘ
use turborand::prelude::*;
let rand = Rng::new();
let value = rand.bool();
Sample a value from a list:
ⓘ
use turborand::prelude::*;
let rand = Rng::new();
let values = [1, 2, 3, 4, 5];
let value = rand.sample(&values);
Generate a vector with random values:
ⓘ
use turborand::prelude::*;
use std::iter::repeat_with;
let rand = Rng::new();
let values: Vec<_> = repeat_with(|| rand.f32()).take(10).collect();
§Features
The base crate will always export the TurboCore
, GenCore
,
SeededCore
, TurboRand
, SecureCore
and ForkableCore
traits, and will do
so when set as default-features = false
in the Cargo.toml. By default,
it will have wyrand
feature enabled as the basic PRNG exposed.
alloc
- Enables support for boxedTurboCore
references, as well asTurboRand
methods that returnVec
results.fmt
- Enablescore::fmt::Debug
implementations forrng::Rng
&chacha_rng::ChaChaRng
.std
- Enablesstd
features, such asalloc
methods as well asDefault
implementations forrng::Rng
&chacha_rng::ChaChaRng
.wyrand
- Enablesrng::Rng
, so to provide a basic, non-threadsafe PRNG. Enabled by default.no-std
compatible.atomic
- Enablesrng::AtomicRng
, so to provide a thread-safe variation ofrng::Rng
. Enableswyrand
feature implicitly. Note, this is slower thanrng::Rng
.rand
- Providescompatibility::RandCompat
, which implementsRngCore
so to allow for compatibility withrand
ecosystem of cratesserialize
- EnablesSerialize
andDeserialize
derives onrng::Rng
,rng::AtomicRng
andchacha_rng::ChaChaRng
, provided they have their respective features activated as well.chacha
- Enableschacha_rng::ChaChaRng
for providing a more cryptographically secure source of Rng. Note, this will be slower thanrng::Rng
in throughput, but will produce much higher quality randomness.no-std
compatible.
Modules§
- chacha_rng
chacha
A cryptographically secure PRNG (CSPRNG) based on ChaCha8. - compatibility
rand
Compatibility shims for therand
crate ecosystem. - Convenience re-export of common traits, structs and utils.
- rng
wyrand
oratomic
A fast but not cryptographically secure PRNG based on Wyrand.
Enums§
- Enum for determining the kind of PRNG, whether a fast one, or a slow, possibly crypographically secure one.
Traits§
- Trait for enabling creating new
TurboCore
instances from an original instance. Similar to cloning, except forking modifies the state of the original instance in order to provide a new, random state for the forked instance. This allows for creating many randomised instances from a single seed in a deterministic manner. - This trait provides the means to easily generate all integer types, provided the main method underpinning this is implemented:
GenCore::gen
. Once implemented, the rest of the trait provides default implementations for generating all integer types, though it is not recommended to override these. - A marker trait to be applied to anything that implements
TurboCore
in order to indicate that a PRNG source is cryptographically secure, so being a CSPRNG. - Trait for implementing Seedable PRNGs, requiring that the PRNG implements
TurboCore
as a baseline. Seeds must beSized
in order to be used as the internal state of a PRNG. - Base trait for implementing a PRNG. Only one method must be implemented:
TurboCore::fill_bytes
, which provides the basis for any PRNG, to fill a buffer of bytes with random data. - Extension trait for automatically implementing all
TurboRand
methods, as long as the struct implementsTurboCore
&GenCore
. All methods are provided as default implementations that build on top ofTurboCore
andGenCore
, and thus are not recommended to be overridden, lest you potentially change the expected outcome of the methods.