mirror of https://github.com/mkerrisk/man-pages
Because RAND_MAX is equal to INT_MAX, the following expression contained in the manpage for rand(3) is slightly incorrect. j=1+(int) (10.0*rand()/(RAND_MAX+1.0)); The correct expression should use parentheses to group the division before the multiplication, thus yielding: j=1+(int) (10.0*(rand()/(RAND_MAX+1.0))); This is not an error where 10.0 is a floating point number, however where 10.0 is replaced with an integer, this will cause the expression to always evaluate to 1. (The addition of two parentheses would make this bug a lot more difficult to make.)
This commit is contained in:
parent
3876c0e522
commit
6e657687f9
|
@ -130,13 +130,13 @@ p. 277)), the following comments are made:
|
|||
always do it by using high-order bits, as in
|
||||
.RS
|
||||
.sp
|
||||
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
|
||||
j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));
|
||||
.sp
|
||||
.RE
|
||||
and never by anything resembling
|
||||
.RS
|
||||
.sp
|
||||
j=1+(rand() % 10);
|
||||
j = 1 + (rand() % 10);
|
||||
.sp
|
||||
.RE
|
||||
(which uses lower-order bits)."
|
||||
|
|
Loading…
Reference in New Issue