From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: Random implementation Date: Tue, 27 Jan 1998 23:06:58 -0500 Organization: Two pounds of chaos and a pinch of salt. Lines: 39 Message-ID: <34CEAEE2.2DCF@cs.com> References: <6am4ri$dtk$2 AT herald DOT Mines DOT EDU> NNTP-Posting-Host: ppp230.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Jean-Luc Romano wrote: > > My question should be simple enough: > > What is the best way to implement random numbers? Easiest: Use the library code provided for you by the compiler. Best: Implement a custom random number generator from a work like Knuth. > I could use rand() by seeding the srand(seed) function, > but if my seed is always the same, the same random numbers > will be generated. Of course. That's why you use a unique value like the system clock. > I could seed the function with time(NULL) like so: srand(time(NULL)); > but (I think) that seeds the generator based on the system seconds, > so one out of every 60 runs will produce the same random numbers. time_t is defined by the library code as an unsigned integer, representing the number of seconds elapsed since 00:00:00 1/1/1970. Since srand() and its counterpart srandom() take integer arguments, no two values returned by time() will be identical until the year 2106 (2^32 / 365 * 24 * 60 * 60 + 1970). I think that's random enough. P.S.: To be absolutely precise, the period of time_t is 136 years, 70 days, 6 hours, 28 minutes, and 15 seconds. This doesn't translate perfectly into the calendar because of leap years, and I'm not going to try to figure it out. :-) -- --------------------------------------------------------------------- | John M. Aldrich | "To be 'matter of fact' about the | | aka Fighteer I | world is to blunder into fantasy-- | | mailto:fighteer AT cs DOT com | and dull fantasy at that, as the real| | http://www.cs.com/fighteer | world is strange and wonderful." -LL | ---------------------------------------------------------------------