10 ms·
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 sma
by conistonwater 12y ago
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.
- clarry 12y agoDoesn't matter what your RAND_MAX is if you're taking the result modulo anything that's not a power of two.
- conistonwater 12y agoNo, 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.
- christianmann 12y agoAnd if you have a need for it to be precisely correct, I have bad news for you about the word "random".
- clarry 12y agoI have no idea what I was thinking, but I stand corrected. Thanks.
- colmmacc 12y agoIt'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.
- conistonwater 12y agoYes, you are right, I didn't think of really large moduluses of magnitude comparable to RAND_MAX.