Hacker News new | ask | show | jobs
by StefanKarpinski 4259 days ago
This comparison should be taken with some rather large grains of salt...

`(float)rand()/(float)RAND_MAX` is not a good way to generate random floating-point values in C. The values will be in [0,1] but they will not be perfectly uniform. rand() returns a positive `int` and thus only uses about 31-bits of entropy. This is both too much and too little: C floats in [0,1) only have 22 bits or entropy, so you're wasting entropy; on the other hand, all the other codes produce random 64-bit floats (i.e. C doubles), which have 52 bits of entropy, so the C code is generating only 60% as much entropy as the other codes. Instead one should use a function like drand48 which produces a 64-bit C double.

These implementations are all using very different pseudo-random number generation algorithms with very different performance characteristics and qualities. The best is likely the Mersenne Twister algorithm used by PHP – that's the gold standard for non-cryptrographic PRNGs. Others use linear congruential and xor-shift algorithms, which are faster and not as good (easier to distinguish form true randomness).

2 comments

Some variants of XorShift (including the one used by Rust, I believe) are not only faster and much smaller than MT19973 but also produce higher quality numbers. See http://xorshift.di.unimi.it/
Thanks! That's really interesting.
You're just trying to justify not having to make a Julia version of this, don't you? :P