delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/01/17:30:25

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Generating random numbers...
Date: Mon, 1 Jun 1998 21:13:57 +0100
Organization: None
Message-ID: <vNfcpHAFuwc1EwJq@talula.demon.co.uk>
References: <19980527010023 DOT AAI19094 AT ppp114 DOT cartsys DOT com>
<35720B76 DOT C80EFBB AT earthlink DOT net DOT nospam>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 26
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Ryan writes:
>What i did, which was probably a bit of overkill in guaranteeing
>randomness, was seed the number a bunch of times during initialization
>it looks like this:
>
>uclock() // to start timing
>for (i = 0; i <= 4 ; i++){
>   for (i = rand() % 2000; i >= 1600 ; i--);
>   srand(uclock() + biostime());
>}

This is redundant, becuase each call to srand() totally replaces the
existing state of the RNG, so only your very last call will have any
effect. It also doesn't make much sense to use uclock() for this,
because that returns the time delay from when it was first called, which
will always be zero if you run this code at the very start of your
program.

By far the most usual and portable way to seed the system is simply to
call srand(time(NULL)). That works perfectly except in the (unlikely)
case that your program is run multiple times within a single second.


--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
"Miracles are nothing if you've got the wrong intentions" - Mike Keneally

- Raw text -


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