Open-source Rust library

Random numbers for Rust, from microcontrollers to Monte Carlo

vrd generates random numbers with Xoshiro256++ by default, and adds Mersenne Twister, PCG, ChaCha20 and quasi-random sequences when you need them.

  • Linux
  • macOS
  • Cortex-M (no_std)
  • WebAssembly
  • NEON and AVX2

What it does

One crate for the random numbers a program needs

Each card opens its section on the features page.

Xoshiro256++ by default

Random::new() seeds a 32-byte Xoshiro256++ generator from the operating system. Bounded integers are unbiased and floats use every mantissa bit.

How the default works

Five backends, one API

Mersenne Twister, PCG32, PCG64 and a ChaCha20 generator for secrets sit behind the same Random type.

Choose a backend

Reproducible sequences

The same seed gives the same sequence. Snapshot a generator with serde, or split off a stream 2128 draws ahead.

Reproduce a run

Distributions

Uniform, normal (Ziggurat), exponential and Poisson built in, and a Distribution trait for your own.

See the distributions

Quasi-random sequences

Halton, Sobol and Van der Corput low-discrepancy sequences for Monte Carlo integration.

Sample evenly

no_std and WebAssembly

With default features off, the core builds for Cortex-M and wasm32 without an allocator, and CI checks both.

Where it runs

Backends

Fast by default, secure when you ask

The default and the alternatives are the same Random type. Only ChaCha20 is suitable for secrets.

Default

Xoshiro256++

32 bytes of state. The default for simulations, games, sampling and tests. Not for secrets.

Compare the backends

pcg feature

PCG32 and PCG64

16 and 32 bytes of state. PCG32 is the fastest backend in the published benchmarks.

See the timings

crypto feature

ChaCha20

A cryptographically secure generator from rand_chacha, for tokens, keys and anything an attacker would want to predict.

When you need it

Install

Random numbers in a few lines of Rust

Add the crate, create a generator and draw. vrd 0.0.12 needs Rust 1.85 or newer.

  • cargo add vrd
  • cargo install vrd
src/main.rs
use vrd::Random;

fn main() {
    // Xoshiro256++, seeded by the operating system
    let mut rng = Random::new();

    let roll = rng.int(1, 6);  // i32 in [1, 6]
    let x = rng.double();      // f64 in [0, 1)
    let pick = rng.choose(&["a", "b", "c"]);
    println!("{roll} {x} {pick:?}");
}
Security

Predictable by design, unless you ask otherwise

The default generator is fast and statistically good, and anyone who sees enough of its output can predict the rest.

For session tokens, API keys, password-reset links and nonces, turn on the crypto feature and use Random::new_secure(). The security page sets out what vrd does and does not promise.

Unsafe code
SIMD module only
Fuzz targets
6
Secure backend
ChaCha20
Constant time
Not promised

In numbers

What the crate ships

Counted from the vrd repository at version 0.0.14.

Generator backends
5
Built-in distributions
4
Quasi-random sequences
3
Runnable examples
43
  • std (default)
  • alloc
  • serde
  • simd
  • pcg
  • crypto
  • quasirandom
  • crush

What is vrd?

vrd is a free, open-source random number generator library for Rust. Its Random type gives you integers in a range, floats, booleans, choices from a slice, shuffles, tokens and samples from four distributions. The default generator is Xoshiro256++, seeded from the operating system; Cargo features add a Mersenne Twister, PCG32 and PCG64, a ChaCha20 generator for secrets, SIMD byte fills and quasi-random sequences. It is dual-licensed under Apache-2.0 or MIT.

Can I use it for passwords and tokens?

Only with the crypto feature and Random::new_secure(). The default generator, like the Mersenne Twister and PCG backends, is not cryptographically secure: anyone who sees enough of its output can predict the rest. The security page explains where the line is.

Does it need the standard library?

No. With default features off, the core builds without the standard library or an allocator, and CI checks it for a Cortex-M target and for WebAssembly. The features page shows how.

Who makes it?

vrd is written by Sebastien Rousseau and its contributors. The source, the issue tracker and every release are on GitHub; the crate is on crates.io. Read more on the about page.