Random number without using the external crate?

I feel compelled to opine that using C's rand is less than great on account of C's rand function being awful, and for promoting the use of unsafe code in cases where you really don't need it.

Oh, that reminds me of another approach: the one used by Doom back in the 90's: have a table of numbers, containing every possible u8 value exactly once, in a randomly permuted order. The random sequence is just reading those numbers one at a time, starting from a random point. It's guaranteed to produce every possible value with a perfectly flat distribution! :smiley:

1 Like

and how do you get this random point? you will need a random number for that right?

... yes...

Both /dev/random and /dev/urandom use a cryptographically secure pseudo-random number generator (PRNG) to generate the numbers. I was surprised by this -- there is a great page about it here: https://www.2uo.de/myths-about-urandom/

3 Likes

Sorry for bumping this 2 year old thread, but there's the recent fastrand if you don't need cryptographically secure random numbers. Zero dependencies as of 1.1.0. I've used it to great success here, and here's another example where it can replace rand.

2 Likes