[][src]Module rand::rngs

Random number generators and adapters

Background: Random number generators (RNGs)

Computers cannot produce random numbers from nowhere. We classify random number generators as follows:

Traits and functionality

All RNGs implement the RngCore trait, as a consequence of which the Rng extension trait is automatically implemented. Secure RNGs may additionally implement the CryptoRng trait.

All PRNGs require a seed to produce their random number sequence. The SeedableRng trait provides three ways of constructing PRNGs:

Use the [rand_core] crate when implementing your own RNGs.

Our generators

This crate provides several random number generators:

The algorithms selected for [StdRng] and [SmallRng] may change in any release and may be platform-dependent, therefore they should be considered not reproducible.

Additional generators

TRNGs: The rdrand crate provides an interface to the RDRAND and RDSEED instructions available in modern Intel and AMD CPUs. The rand_jitter crate provides a user-space implementation of entropy harvesting from CPU timer jitter, but is very slow and has security issues.

PRNGs: Several companion crates are available, providing individual or families of PRNG algorithms. These provide the implementations behind [StdRng] and [SmallRng] but can also be used directly, indeed should be used directly when reproducibility matters. Some suggestions are: rand_chacha, rand_pcg, rand_xoshiro. A full list can be found by searching for crates with the rng tag.

Modules

adapter

Wrappers / adapters forming RNGs

mock

Mock random number generator

Structs

EntropyRngDeprecated

An interface returning random data from external source(s), provided specifically for securely seeding algorithmic generators (PRNGs).

OsRng

A random number generator that retrieves randomness from from the operating system.

SmallRng

A small-state, fast non-crypto PRNG

StdRng

The standard RNG. The PRNG algorithm in StdRng is chosen to be efficient on the current platform, to be statistically strong and unpredictable (meaning a cryptographically secure PRNG).

ThreadRng

The type returned by [thread_rng], essentially just a reference to the PRNG in thread-local memory.