A Rust library for random number generation, featuring:
- Easy random value generation and usage via the
Rng
,SliceRandom
andIteratorRandom
traits - Secure seeding via the
getrandom
crate and fast, convenient generation viathread_rng
- A modular design built over
rand_core
(see the book) - Fast implementations of the best-in-class cryptographic and non-cryptographic generators
- A flexible
distributions
module - Samplers for a large number of random number distributions via our own
rand_distr
and via thestatrs
- Portably reproducible output
#[no_std]
compatibility (partial)- Many performance optimisations
It's also worth pointing out what rand
is not:
- Small. Most low-level crates are small, but the higher-level
rand
andrand_distr
each contain a lot of functionality. - Simple (implementation). We have a strong focus on correctness, speed and flexibility, but not simplicity. If you prefer a small-and-simple library, there are alternatives including fastrand and oorandom.
- Slow. We take performance seriously, with considerations also for set-up time of new distributions, commonly-used parameters, and parameters of the current sampler.
Documentation:
Add this to your Cargo.toml
:
[dependencies]
rand = "0.8.5"
To get started using Rand, see The Book.
Rand is mature (suitable for general usage, with infrequent breaking releases which minimise breakage) but not yet at 1.0. Current versions are:
- Version 0.8 was released in December 2020 with many small changes.
- Version 0.9 is in development with many small changes.
See the CHANGELOG or Upgrade Guide for more details.
Rand is built with these features enabled by default:
std
enables functionality dependent on thestd
liballoc
(implied bystd
) enables functionality requiring an allocatorgetrandom
(implied bystd
) is an optional dependency providing the code behindrngs::OsRng
std_rng
enables inclusion ofStdRng
,thread_rng
andrandom
(the latter two also require thatstd
be enabled)
Optionally, the following dependencies can be enabled:
log
enables logging via log
Additionally, these features configure Rand:
small_rng
enables inclusion of theSmallRng
PRNGnightly
includes some additions requiring nightly Rustsimd_support
(experimental) enables sampling of SIMD values (uniformly random SIMD integers and floats), requiring nightly Rust
Note that nightly features are not stable and therefore not all library and
compiler versions will be compatible. This is especially true of Rand's
experimental simd_support
feature.
Rand supports limited functionality in no_std
mode (enabled via
default-features = false
). In this case, OsRng
and from_os_rng
are
unavailable (unless getrandom
is enabled), large parts of seq
are
unavailable (unless alloc
is enabled), and thread_rng
and random
are
unavailable.
Many (but not all) algorithms are intended to have reproducible output. Read more in the book: Portability.
The Rand library supports a variety of CPU architectures. Platform integration is outsourced to getrandom.
Seeding entropy from OS on WASM target wasm32-unknown-unknown
is not
automatically supported by rand
or getrandom
. If you are fine with
seeding the generator manually, you can disable the getrandom
feature
and use the methods on the SeedableRng
trait. To enable seeding from OS,
either use a different target such as wasm32-wasi
or add a direct
dependency on getrandom
with the js
feature (if the target supports
JavaScript). See
getrandom#WebAssembly support.
Rand is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT, and COPYRIGHT for details.