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.

7 min read • View on GitHub • More from hpenne

A wide editorial illustration showing a small mechanical vault feeding a single stream of numbered tokens into one compact box, while a larger background machine branches tokens into multiple hidden threads. The scene explains the crate's core idea: randomness should be explicit and locally owned, not pulled from ambient thread-local state.
The thesis is visible at a glance. smallrand prefers one owned RNG instance over hidden global convenience.
Key Takeaways

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.

hpenne, Author/Maintainer · Reddit comment on smallrand
A hedcut-style portrait of hpenne based on the verified GitHub avatar. This identifies the project author in the article and ties the design philosophy back to a real maintainer.

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.

Two primitive outputs fan out into most of the crate. The diagram shows how smallrand keeps the API small by centralizing behavior.

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);
A close-up illustration of a small toolbench with two sealed engines, one fast gear cluster and one braided lockbox, sitting above a single lever and a chain of derived tools. It explains how smallrand builds many conveniences from a tiny primitive interface.
The crate is small because the core is small. Most of the API is layered on top of two raw outputs.

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.

CrateUnsafe codeDefault RNG styleAlgorithmsEntropy / seedingBest fit
smallrandNoExplicit local instanceXoshiro256++, ChaCha12OS entropy plus fallbacksSmall, auditable Rust codebases
randYes, in partsOften thread-local or ergonomic defaultsBroad ecosystemRich platform supportGeneral-purpose Rust applications
fastrandYesThread-local convenienceWyrandMinimal seeding storyVery fast non-crypto use
oorandomNoExplicit local instancePCG32Usually manual seedingTiny 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.

PropertysmallrandPerformance-first crates
Unsafe codeForbiddenCommon when optimizing hot paths
Audit burdenLowHigher
Peak throughputGoodUsually higher
API styleExplicit and localOften convenience-first
Primary valueTrust and clarityRaw 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.

CratePhilosophyTrade-offWhy choose it
smallrandExplicit, local, auditable RNGsSome speed is left on the tableYou want a small crate you can trust
randFeature-rich standard library ecosystemMore abstraction and more surface areaYou want the default option with broad reach
fastrandMinimal and fastUses convenience-heavy defaultsYou want raw speed for non-crypto work
oorandomTiny deterministic generatorFewer built-in optionsYou want simplicity above all else