Mail Archives: djgpp/1996/08/29/13:23:47
Francisco Javier Pena Pareja (i32japef AT sun630 DOT uco DOT es) wrote:
: On Wed, 28 Aug 1996, J.J. Pierson wrote:
: > Silly question... How would I go about getting a random number from
say 1
: > to a specified number?
: >
: It's quite simple. Let's suppose you want to get a random number from 0
: to 255. Then:
: rand()%256
: will do that. From 1 to 255?
: rand()%255 + 1
I think this is a bad technique for numerical stuff.
The lower bits of random generators are not reaaly random. One better
scales the rand() as in
low + (rand()/(max_rand)) * (high - low +1)
with low and high describing the interval and max_rand, the maximal
value for the generator in casu.
For games the modulo operator might be sufficient, but certainly not
for simulations.
- Raw text -