Mail Archives: djgpp/1997/05/30/17:48:23
In article <01bc6ce9$72076bc0$363e63c3 AT 8652hvt73761>, "Matthew Bennett" <Bennett AT btinternet DOT com> writes:
+->When using the 'random' command in the form:
+->printf("%d\n", random() & 10);
+->The numbers produced are always either 0, 2, 8 or 10.
Hi,
if you want a random number bitween 0 and N-1 you should use:
random() % N ( % is MODULO )
However, if N equals a power of 2 ( as 16, 128, 1024, etc... )
you can speed it up by using: random() & (N-1)
but this works only if N is a power of 2.
--
Fred.
Frederic Triquet
E-Mail: triquet AT fil DOT univ-lille1 DOT fr
Or: rodger DOT adrenaline AT writeme DOT com
- Raw text -