delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/31/22:16:48

From: Ryan <axle AT earthlink DOT net DOT nospam>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Generating random numbers...
Date: Sun, 31 May 1998 19:01:26 -0700
Organization: N/A
Lines: 50
Message-ID: <35720B76.C80EFBB@earthlink.net.nospam>
References: <19980527010023 DOT AAI19094 AT ppp114 DOT cartsys DOT com>
Reply-To: axle AT earthlink DOT net
NNTP-Posting-Host: pool037-max3.laguna-ca-us.dialup.earthlink.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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());
}

i know that it is probably incredibly inefficient and bad coding and
all of that crap, but it seeds the number generator rand() pretty well
:)
Nate Eldredge wrote:
> 
> At 05:32  5/26/1998 -0500, Jason Cain wrote:
> >Hello,
> >
> >I'm trying to generate a sequence of random numbers, so I wrote a simple
> >program to generate 20 random numbers as a test.  I'm using the
> >srandom() function with a seed value generated by rawclock().  I thought
> >this might be a good way to generate a different seed every time.
> >However, rawclock() seems to be returning zero every time, which causes
> >me to get the same sequence of random numbers every time.  Can anyone
> >offer any suggestions on what I'm doing wrong or alternate methods?  I'm
> >using Windows NT4--perhaps that has something to do with it?  Short code
> >follows.
> 
> `rawclock' is buggy in the current version. Presently, it actually returns
> the number of clock ticks since it was first called. In v2.02, it will work
> like the documentation says.
> 
> You could use `time(NULL)' instead, which is also more portable (ANSI). If
> you expect to run your program more than once a second, you could use the
> `tv_usec' field returned by `gettimeofday', which is POSIX. See the docs.
> 
> Nate Eldredge
> nate AT cartsys DOT com

-- 
"A good american must practice bad Americanism." -Black Like Me

Ryan Bank		O/	Disco Inferno !!
axle AT earthlink DOT net      /\	Burn, baby burn!!!
Laguna Niguel, CA        />	Disco all night!

Pursuant to US Code, Title 47, Chapter 5, Subchapter II, B227, any and
all nonsolicited commercial e-mail sent to this address is subject to a
download and archival fee in the amount of $500 US. E-mailing denotes
acceptance of these terms.

- Raw text -


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