Mail Archives: djgpp/1994/09/26/12:19:53
On my response to you I wrote:
>For your application, they recommend the following:
>
> roll += int(rand()*_sides) + 1;
>
>This implementation is also attractive because it does not involve a divide
(the >%) and therefore should be somewhat faster.
which is in error. I was adapting the fortran version of which ran()
returns a float between 0 and 1. The C rand() returns an int between 0 and
RAND_MAX. The above code assumes that rand returns a float between 0 and 1.
You may change the above for C to
roll += int(float(rand())/(RAND-MAX+1) * _sides) + 1;
This still should not involve a divide since the /(RAND_MAX+1) is a constant
and would be implemented as a multiply by 1/(RAND_MAX+1).
Sorry for the error.
Richard Young,
Signal Processing Engineer,
Communications Research Centre,
Ottawa, Canada.
- Raw text -