delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/09/20:05:20

Date: Thu, 9 Oct 1997 17:03:20 -0700 (PDT)
Message-Id: <199710100003.RAA14864@adit.ap.net>
Mime-Version: 1.0
To: Antonio Dias <accdias AT provider DOT com DOT br>, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: Random numbers again...

At 04:20  10/7/1997 -0300, Antonio Dias wrote:
>Hi all,
>
>I was looking all that messages about random numbers and I decided to
>investigate. I discover that to generate a true random number you must
>use ramdom() function instead of rand(). 
This is wrong. rand() and random() both generate random numbers, in fact
they are the same code under DJGPP. rand() generates a number between 0 and
RAND_MAX and comes from ANSI (IIRC), and random() generates a number between
0 and MAXINT and comes from BSD. It so happens that RAND_MAX == MAXINT under
DJGPP.
>These functions need to be
>initialized with a seed to work well. This is acomplished using
>srandom() function. The srandom() function expects a int as seed and if
>your program always send the same seed it will *always* get the same
>ramdom numbers.
If you use rand(), use the srand() function to seed it. rand() is probably
preferable since it is ANSI standard and more portable.
> The problem now consists in pass a new seed to srandom
>everytime you start your program. I acomplish this using the hundreds of
>seconds from machine's internal clock. Look the code below:
>[snipped]
That works, but srand(time(0)) is probably just as effective and much simpler.

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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