delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/04/09:13:25

Message-Id: <199802041410.QAA04887@ankara.duzen.com.tr>
Comments: Authenticated sender is <mitch AT ankara DOT duzen DOT com DOT tr>
From: "S. M. Halloran" <mitch AT duzen DOT com DOT tr>
Organization: User RFC 822- and 1123-Compliant
To: djgpp AT delorie DOT com
Date: Wed, 4 Feb 1998 16:11:26 +0200
MIME-Version: 1.0
Subject: Re: Automatically seeding random numbers
In-reply-to: <34D80662.CD7C1FC@ea.oac.uci.edu>

>   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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019