News for hackers. Stuff that matters.
Re:Random in the wild by conistonwater on Tuesday December 09, 2014 @03:38PM (#8725549)
On my machine RAND_MAX is 2^31-1. So getting the probability wrong in the way you describe means a relative error of one in two billion. That's really quite small for non-critical applications.
Re:Random in the wild by colmmacc on Tuesday December 09, 2014 @04:05PM (#8725735)
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.

Re:Random in the wild by conistonwater on Tuesday December 09, 2014 @04:38PM (#8725962)
Yes, you are right, I didn't think of really large moduluses of magnitude comparable to RAND_MAX.
Re:Random in the wild by clarry on Tuesday December 09, 2014 @03:46PM (#8725599)
Doesn't matter what your RAND_MAX is if you're taking the result modulo anything that's not a power of two.
Re:Random in the wild by conistonwater on Tuesday December 09, 2014 @03:57PM (#8725675)
No, it does matter. If RAND_MAX is really large, the relative error in "incorrect" probabilities is really small. Unless you have a critical need for it to be precisely correct, the error is basically negligible.
Re:Random in the wild by clarry on Wednesday December 10, 2014 @07:46AM (#8728862)
I have no idea what I was thinking, but I stand corrected. Thanks.
Re:Random in the wild by christianmann on Wednesday December 10, 2014 @02:22AM (#8728071)
And if you have a need for it to be precisely correct, I have bad news for you about the word "random".