API Reference

rand

Random number generation for Rust with distributions, RNG traits, and deterministic seeded generators.

Official Documentation ↗
CompatibilityThis documentation covers version v0.8. Check the official docs for the latest API surface.

Installation

cargocargo add rand

Methods

rand separates random sources from distributions and lets you seed generators for tests.

Key Features

  • Thread-local and seeded generators
  • Uniform, range, and weighted distributions
  • Deterministic output with a fixed seed

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Security

Official security statements captured from the package documentation:

  • ThreadRng 与 StdRng 使用同一 PRNG,并自动由 OsRng 播种 — “ThreadRng uses the same PRNG as StdRng for security and performance and is automatically seeded from OsRng.” (source: https://docs.rs/rand/0.8.5/rand/rngs/struct.ThreadRng.html, fetched 2026-11-08T17:53:50+08:00)
  • 每 64 kiB 随机数据(以及 Unix fork 后)重新播种,作为对抗侧信道与被误用的额外预防措施,PRNG 算法假定安全 — “Unlike StdRng, ThreadRng uses the ReseedingRng wrapper to reseed the PRNG from fresh entropy every 64 kiB of random data as well as after a fork on Unix (though not quite immediately; see documentation of ReseedingRng). Note that the reseeding is done as an extra precaution against side-channel atta” (source: https://docs.rs/rand/0.8.5/rand/rngs/struct.ThreadRng.html, fetched 2026-11-08T17:53:50+08:00)

API reference evidence

Fetched from official sources on 2026-11-08T17:53:50+08:00 via WebFetch https://docs.rs/rand/0.8.5/rand/fn.thread_rng.html.

FAQ

How do I get reproducible randomness?
Use a seeded generator such as StdRng::seed_from_u64 and thread it through your code so tests are deterministic.
How do I pick from a weighted set?
Build a WeightedIndex distribution and sample from it with the generator, avoiding manual probability math.
Is thread_rng suitable for crypto?
It is a CSPRNG backed by the OS, but for key material prefer an OsRng from the getrandom-backed source.