Mail Archives: djgpp/1998/02/04/09:13:25
> Basically, I'd like a program to (automatically) seed srand() with
> a
> different value each time the program is run. (Currently I require
> the user to type in a seed, which is a nuisance.) Initially, I
> thought something as simple as srand(clock()) would work, but then I
> found out that clock() only returns the number of clock ticks since
> the *last* call of clock(), so clock() always returns the value 0
> when called for the first time.
Try getting the time of the system clock and use that for a seed. I
have done that and the random number list seems to vary. The
use of the functions in 'info gcc' (read from RHide) doesn't seem to
be very well documented...for example, the time() function. C'est la
vie.
#include <time.h>
#include
func()
{
time_t sysclock; /* will load # of seconds since 1/1/1970 UTC */
long myrandomnumber;
....
time(&sysclock);
srandom(sysclock); /* djgpp is using the srandom() function,
a contest between Berkeley and AT&T, I think*/
random();
myrandomnumber = random(); /* C library of djgpp also features
the rand() function, but cycle differs */
....
}
I think there was a thread not too long ago about random() vs.
rand(). Check mail archives.
Mitch Halloran
Research (Bio)chemist
Duzen Laboratories Group
Ankara TURKEY
mitch AT duzen DOT com DOT tr
other job title: Sequoia's (dob 12-20-95) daddy
- Raw text -