[OCLUG-devel] Limiting range of random numbers
Tim Thelin
tthelin at sbcglobal.net
Fri Nov 19 18:17:45 PST 2004
James Colannino wrote:
> James Colannino wrote:
>
>> Hey everyone,
>>
>> I'm using the following code snippet to generate suedo-random
>> numbers[...]
>
>
>
> Ok, well, after playing around a bit and trying a few random things, I
> think I managed to stumble across a possible solution. If I return
> (number % 20) instead of just number, it seems to always give me a
> value between 0 and 19. I'm not quite sure why this works (if someone
> could tell me how that would be great) but hey, at least it's working :)
>
> James
That's because x % y is modulo math. The expression returns the
remainder of x / y.
So
4 % 2 == 0
5 % 2 == 1
6 % 2 == 0
9 % 3 == 0
11 % 3 == 2
And any number X in the expression X % 20 will yield a number between 0
and 19.
Tim
More information about the OCLUG-devel
mailing list