smallrand: The Rust RNG Crate That Refuses to Hide the State
A tiny, no-unsafe alternative to `rand` that favors explicit seeding, auditable internals, and a surprisingly opinionated API.
- smallrand argues that randomness is easier to trust when the RNG is carried explicitly instead of borrowed from hidden thread-local state.
- Its smallest meaningful design choice is also its sharpest one: two primitive outputs power almost every higher-level helper in the crate.
- The crate trades some peak performance for a codebase that is easier to audit, simpler to seed, and easier to reason about in async-heavy code.
- In the Rust RNG landscape, smallrand sits between the sprawling `rand` ecosystem and the ultra-minimal speed-first crates.
The quiet rebellion against thread-local RNGs
`smallrand` is not trying to outgrow `rand`. It is trying to disagree with it in one specific way that matters: randomness should be an explicit dependency, not an ambient service. The crate starts from the idea that you should own the RNG instance, seed it on purpose, and pass it where it is needed.
That sounds like a style preference until you put it in async code, long-lived services, or anything you want to audit later. Hidden thread-local state is convenient, but it also makes behavior harder to trace, harder to reproduce, and harder to reason about when tasks move across execution contexts.
The other motivation was that I'm not a big fan of how rand's API encourages you to use thread local RNG instances. It creates unnecessary (thread) global state, which is almost always a bad idea. smallrand doesn't have any global state by default.
A tiny trait surface that pushes everything downstream
The core engineering trick is simple: the trait surface is tiny. `smallrand` only requires two primitive methods, `random_u32` and `random_u64`, then builds the rest from there. That keeps the public shape compact while letting the crate offer floats, ranges, shuffling, and filling without multiplying abstractions.
use smallrand::{Rng, Xoshiro256pp};
let mut rng = Xoshiro256pp::new();
let n: u64 = rng.random();
let r = rng.range(10..100);
let mut buf = [0u8; 32];
rng.fill(&mut buf);
Two engines, two jobs
`smallrand` keeps the algorithm choice simple on purpose. `Xoshiro256++` is the fast, non-cryptographic engine. `ChaCha12` is the stronger option when you want a stream-cipher-based generator with a security story that is far more serious than a toy PRNG.
| Crate | Unsafe code | Default RNG style | Algorithms | Entropy / seeding | Best fit |
|---|---|---|---|---|---|
| smallrand | No | Explicit local instance | Xoshiro256++, ChaCha12 | OS entropy plus fallbacks | Small, auditable Rust codebases |
| rand | Yes, in parts | Often thread-local or ergonomic defaults | Broad ecosystem | Rich platform support | General-purpose Rust applications |
| fastrand | Yes | Thread-local convenience | Wyrand | Minimal seeding story | Very fast non-crypto use |
| oorandom | No | Explicit local instance | PCG32 | Usually manual seeding | Tiny deterministic workloads |
That contrast matters because it turns a hard choice into a readable one. If you want speed and a simple non-crypto generator, the crate gives you that. If you want a stronger engine, it gives you that too, without making the API feel like a maze.
How smallrand gets entropy without turning into a platform mess
The seeding path is cautious without becoming ornate. On Unix, it can read from `/dev/urandom`. On platforms that need it, it uses `getrandom`. If those paths are not available, it still has a fallback story through `HashMapEntropy`, and `SecureEntropy` adds runtime health tests to catch obvious failure modes.
That is the part that keeps the crate from feeling naive. Minimalism is only persuasive when it still respects the messy reality of entropy sources, platform differences, and the possibility that a source can fail in practice.
use smallrand::{SecureEntropy, Xoshiro256pp};
let entropy = SecureEntropy::default();
let mut rng = Xoshiro256pp::from_entropy(entropy);
let value = rng.random_u64();
Why no unsafe is the real product claim
The headline feature is not just small size. It is the decision to forbid unsafe code. That is a trade. You give up some of the tricks that make the fastest RNG crates faster, but you gain a codebase that is much easier to review, reason about, and ship into a security-conscious environment.
For many projects, that is the real budget line. If the crate is part of a simulation, a service, or a reproducible test harness, the difference between "fastest possible" and "easy to audit" is not academic. It is the difference between a tool you trust and a tool you keep rechecking.
| Property | smallrand | Performance-first crates |
|---|---|---|
| Unsafe code | Forbidden | Common when optimizing hot paths |
| Audit burden | Low | Higher |
| Peak throughput | Good | Usually higher |
| API style | Explicit and local | Often convenience-first |
| Primary value | Trust and clarity | Raw speed |
Where smallrand sits in the Rust RNG landscape
The easiest way to place `smallrand` is as a rebuttal to two extremes. Against `rand`, it says the ecosystem can be smaller, more direct, and less dependent on implicit thread-local behavior. Against speed-first crates, it says safety and auditability are worth protecting even when the benchmark crown is not the goal.
That makes it a narrow crate in the best sense. It is not trying to be everything. It is trying to be the thing some teams actually need: a small RNG library with familiar algorithms, explicit state, no unsafe code, and enough entropy plumbing to be usable in real projects.
| Crate | Philosophy | Trade-off | Why choose it |
|---|---|---|---|
| smallrand | Explicit, local, auditable RNGs | Some speed is left on the table | You want a small crate you can trust |
| rand | Feature-rich standard library ecosystem | More abstraction and more surface area | You want the default option with broad reach |
| fastrand | Minimal and fast | Uses convenience-heavy defaults | You want raw speed for non-crypto work |
| oorandom | Tiny deterministic generator | Fewer built-in options | You want simplicity above all else |





