Mail Archives: djgpp/1998/06/11/01:46:58
On Wed, 10 Jun 1998 21:59:57 -0400 in comp.os.msdos.djgpp Nicolas
Blais <eletech AT netrover DOT com> wrote:
: Hi, the function below seams to always give me the same set of numbers
: in R_C everytime I run it, can it be fixed?
: srandom(clock()/CLOCKS_PER_SEC);
: int R_C = random() / (RAND_MAX / 255 + 1);
Yes. If you look at the documentation for `clock' you'll see that it
returns the time since you first called it; i.e. the first time you
call it it will return `0'. So you're not seeding with different
numbers.
A better function to use to get a different number each run is the
`time' function. You can use it like this:
#include <time.h>
#include <stdlib.h>
...
srandom (time (NULL));
`time' counts up in seconds in djgpp, so provided your runs are
separated by more than one second you'll get a different sequence each
time.
If that's not good enough you still have a few more options, but
generally `time' is sufficient.
--
george DOT foot AT merton DOT oxford DOT ac DOT uk
xu do tavla fo la lojban -- http://xiron.pc.helsinki.fi/lojban/lojban.html
- Raw text -