Mail Archives: djgpp/1996/11/30/17:18:24
Murray Stokely wrote:
>
> Whats the best way to generate a random number from 0 to 500? I
> tried the x=random(500) (borland way) and i got some huge number.
> After reading up on it I found out that random() always returns a #
> between 0 and MAX_INT_SIZE how do i create a much smaller random #?
The canonical answer has tended to be
x=random(seed)%500
Where % returns the modulus (remainder) after dividing by 500.
Seed is usually pulled from the system clock or keyboard timer or
whatever.
On the other hand, this is probably a non-runtime-speed-optimal way of
generating random numbers...you could generate a look-up table during
initialization, maybe...
Roger Carbol .. rog AT col DOT ca .. order in chaos
- Raw text -