Mail Archives: djgpp/1997/11/26/15:54:51
At 05:28 11/25/1997 GMT, George Foot wrote:
>Also note that if you're from a Borland background you'll be expecting
>the `random' function to take a parameter (the smallest integer that
>will never be returned) -- in djgpp this is not the case. The `rand'
>function returns an integer from 0 to RAND_MAX and the `random'
>function returns an integer from 0 to MAXINT. In practice, these
>maxima are both the same, and equal to the largest number that can be
>expressed as a (32-bit) integer. To restrict the range of the
>numbers, use the divison-remainder operator, `%'.
I'll make the usual comment here that for the `rand' RNG, the low-order
bits, which are what you get when using `%', are sometimes not as random as
one would like. You can fix this with some casts:
(int)((double)rand()/RAND_MAX)*largest_random_number_you_want
`random' doesn't have this problem, but also is not ANSI. The discrepancy is
due to legal issues and is being considered.
Nate Eldredge
eldredge AT ap DOT net
- Raw text -