Crate nanorand[][src]

Expand description

A library meant for fast, random number generation with quick compile time, and minimal dependencies.


Generating a number with an initialized RNG

use nanorand::{Rng, WyRand};

let mut rng = WyRand::new();
println!("Random number: {}", rng.generate::<u64>());

Generating a number with a thread-local RNG

use nanorand::Rng;

let mut rng = nanorand::tls_rng();
println!("Random number: {}", rng.generate::<u64>());

Generating a number in a range

use nanorand::{Rng, WyRand};

let mut rng = WyRand::new();
println!("Random number between 1 and 100: {}", rng.generate_range(1_u64..=100));
println!("Random number between -100 and 50: {}", rng.generate_range(-100_i64..=50));

Shuffling a Vec

use nanorand::{Rng, WyRand};

let mut rng = WyRand::new();
let mut items = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
rng.shuffle(&mut items);

Why should I use this over…

  • rand - The standard rand crate is a complex beast. It contains unsafe code in the core implementations, and while it has much more options than we do, that’s kind of the point. We’re straight to the point, while rand is everything and the kitchen sink.
  • fastrand, oorandom, random-fast-rng, or randomize - These are all minimal, zero-dep implementations of the PCG family of RNGs (Pcg32 and Pcg64). While these are decent, they are much slower than wyrand (which beats the speed of these Pcg32 implementations while providing 64 random bits), and do not provide CSPRNGs.
  • getrandom - The getrandom crate just provides OS entropy sources. It is not meant for random number generation. In fact, we provide it as an optional entropy source.

RNG Implementations

RNGnanorand typeOutput SizeCryptographically SecureSpeed1NotesOriginal Implementation
wyrandnanorand::WyRand, nanorand::tls::TlsWyRand64 bits (u64)🚫16.4 GB/s
Pcg64nanorand::Pcg6464 bits (u64)🚫1.6 GB/s
ChaChananorand::ChaCha512 bits ([u32; 16])204 MB/s (ChaCha8), 79 MB/s (ChaCha20)Only works in Rust 1.47 or above

1. Speed benchmarked on an M1 Macbook Air

Entropy Sources

Listed in order of priority

  • If the getrandom feature is enabled, then getrandom::getrandom will be called.
  • If the rdseed feature is enabled, and is running on an x86(-64) system with the RDSEED instruction, then we will attempt to source as much entropy as possible via our rdseed_entropy function
  • Linux and Android will attempt to use the getrandom syscall.
  • macOS and iOS (Darwin-based systems) will use Security.framework’s SecRandomCopyBytes.
  • Windows
    • If we’re targeting UWP, then the BCryptGenRandom is used with system-preferred RNG (BCRYPT_USE_SYSTEM_PREFERRED_RNG).
    • Otherwise, we’ll use RtlGenRandom.

Feature Flags

  • std (default) - Enables Rust std lib features, such as seeding from OS entropy sources.
  • tls (default) - Enables a thread-local WyRand RNG (see below). Requires tls to be enabled.
  • wyrand (default) - Enable the WyRand RNG.
  • pcg64 (default) - Enable the Pcg64 RNG.
  • chacha - Enable the ChaCha RNG. Requires Rust 1.47 or later.
  • rdseed - On x86 and x86-64 platforms, the rdseed intrinsic will be used when OS entropy isn’t available.
  • zeroize - Implement the Zeroize trait for all RNGs.
  • getrandom - Use the getrandom crate as an entropy source. Works on most systems, optional due to the fact that it brings in more dependencies.


pub use gen::*;
pub use rand::*;
pub use tls::tls_rng;


Implementation of cryptography, for CSPRNGs.

Sources for obtaining entropy.

Traits for generating types from an RNG.

RNG algorithms.

Provides a thread-local WyRand RNG.