3 ms·
My biggest use case for random numbers is for fuzz testing. I fuzz test all over the place - any time I have some clear invariants and some complex code to test
by josephg 3y ago
My biggest use case for random numbers is for fuzz testing. I fuzz test all over the place - any time I have some clear invariants and some complex code to test, I’ll generate random data in a loop and make sure the invariants always hold. This finds so many bugs.
But for this kind of work, a good prng is better. The reason is simple: when I find a failing test, I can print out the seed that generated that test. Then I can rerun just that one seed (so the error shows up immediately) and debug. Every time I rerun my program, a fixed seed means it will use the same input and fail in the same way. Perfect.
High quality, modern PRNGs shouldn’t have the kind of bugs that cause the problem listed at the start of this article. I think part of the problem is that C has so many ways to get random numbers that it’s hard to tell which are high quality, and on which platforms.
Rust’s rand crate is the poster child for me of what this looks like done well. Here’s a bunch of prngs and csrngs, implemented and listed in a simple table for you to pick which one you want to use. For each rng they show rough performance numbers and the prngs have a rough quality guide:
https://rust-random.github.io/book/guide-rngs.html https://rust-random.github.io/book/guide-rngs.html
And if in doubt, thread_rng() is always a good choice.
- ngneer 3y agoI recommend you research CSPRNGs before arriving at a conclusion. They, too, are seeded, which is helpful for deterministically replaying a sequence, say for fuzzing.
- josephg 3y agoOh, TIL! And indeed chacha in Rust’s rand crate can be seeded. Cool! https://rust-random.github.io/rand/rand_chacha/struct.ChaCha20Rng.html#impl-SeedableRng-for-ChaCha20Rng https://rust-random.github.io/rand/rand_chacha/struct.ChaCha...