TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Mike Bilow
from: Sean Price
date: 1996-05-19 10:45:16
subject: Random Seed

MB> Be careful: srand() takes an unsigned int, while time() 
 MB> returns a time_t.  These are different sizes on many 
 MB> compilers.  To handle this portably, do something like this:

 MB>    srand((unsigned int)(time(NULL) % UINT_MAX));

        Yeah, I did something like that.  Although I ran the time() result
through the ELF Hashh  first, and used INT_MAX.

 MB> If you need to guarantee uniform distribution of inputs 
 MB> to srand(), the proper solution is to apply a hash 
 MB> (such as the ELF hash I discussed in a prior message to 
 MB> you) and a prime modulus less than INT_MAX before 
 MB> feeding the seed to srand().

        Why a prime modulus?

 MB> Note especially that the random number generators 
 MB> provided with most compilers are awful.  Borland's and 
 MB> Watcom's are particularly bad, since even their 32-bit 
 MB> OS/2 compilers only return 16 bits of significance from 
 MB> rand().  ANSI C guarantees that RAND_MAX is at least 
 MB> 32767, and Watcom gives you the bare minimum.  Just 
 MB> because rand() returns a 32-bit int does not 
 MB> necessarily imply that the most significant 16 bits are 
 MB> ever non-zero.


        Interesting.  Fortunately, this application doesn't really require
a good random number generator.  The program is just a simple text based
background bitmap changer.

Sean


--- Maximus/2 3.01
* Origin: Sanctuary from The Law BBS (1:205/46)
SEEN-BY: 50/99 270/101 620/243 711/401 409 410 413 430 808 809 934 955
SEEN-BY: 712/407 515 517 628 713/888 800/1
@PATH: 205/46 500 1 270/101 712/515 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.