Urgent.News

What's breaking now, across thousands of outlets.

Business

Why I forked rand

The rand crate is the go-to option for generating randomness in the Rust ecosystem. However, the author found many annoying details and decided to create their own crate, urandom. urandom offers a more focused experience with fewer public and implementation points, focusing on a specific user experience.

The author noticed that discovering the right method for useful operations in rand can be difficult. IDE completion does not always help, as the method may belong to the RNG, the slice, the iterator, or elsewhere. urandom puts its high-level consumer API on one Random wrapper struct, making editor completion show relevant methods like random, uniform, chance, choose, shuffle, sample, and more.

urandom restricts its Rng trait to be sealed, allowing only selected generators to be implemented inside the crate. This provides a better fit for compatibility with other projects, specialized hardware, or simulation-specific generators. Sealing Rng enables shaping the generator around the crate, adding the necessary primitives without creating an implementation contract for unknown generators.

The author kept the custom generators and algorithms as narrow as possible, preserving the extension point for advanced users. urandom uses Xoshiro256 for non-cryptographic use and ChaCha12 for cryptographic use, with specialized next_f32 and next_f64 methods for faster random float generation.

A benchmark comparing urandom 1.0 with rand 0.10.2 showed about 31% higher throughput (24% faster) for Xoshiro f64 benchmark. Uniform integer sampling in urandom hides complexity, ensuring unbiased results for most practical ranges without introducing bias. The implementation remains unbiased even when handling one-off ranges, offering a faster and unbiased solution compared to rand's default features.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at casualhacks.net →

More in Business

More from Friday 31 July →