Mail Archives: djgpp/1997/05/31/19:21:21
Matthew Bennett (Bennett AT btinternet DOT com) wrote:
: printf("%d\n", random() & 10);
Use % instead of &, i.e. random()%n gives a number from 0 to n-1. If n is
a power of two you can use random()&(n-1) with the same effect, although
the compiler ought to do this automatically for constant ranges.
: prinf("%d\n", random(10));
This is just plain wrong, random takes no parameters and returns a number
from 0 to MAXINT (over two thousand million).
: For what I'm writing, it would also need to be seeded (the 'srandom'
: command is what I'm currently using).
#include <time.h> and then put srandom(time(0)) to seed it reasonably
well. Note that you should also #include <stdlib.h> for the random and
srandom functions.
--
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford
- Raw text -