Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's not so simple. The larger the modulus, the larger the error; up to a limit. If the modulus is 2^30 + 2^29 for example, then values of r in the range 0 ... 2^29 will be represented twice as often as values in the range (2^29 + 1) ... (2^30 + 2^29). This is much more significant error than one in two billion (it's close to 1 in 3, nearly ten orders of magnitude more significant).

Subtleties like this are hard to detect on code review, and even in testing ... the real lesson may be that the very interface behind random(void) is just broken. It really should be random(int) and take care of all of this for you, as many other languages/libraries do.



Yes, you are right, I didn't think of really large moduluses of magnitude comparable to RAND_MAX.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: