delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/26/11:01:11

From: Thomas Harte <T DOT Harte AT btinternet DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: srand()
Date: Sun, 26 Sep 1999 14:37:25 +0100
Organization: BT Internet
Message-ID: <37EE2195.B2BCC4E2@btinternet.com>
References: <7shlcr$bn2$1 AT canopus DOT cc DOT umanitoba DOT ca> <37ec69fb DOT 17055060 AT news DOT uswest DOT net> <37ECFF5F DOT AAD52476 AT btinternet DOT com> <7sj2oj$256$1 AT canopus DOT cc DOT umanitoba DOT ca>
NNTP-Posting-Host: host5-171-230-134.btinternet.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.6 [en-gb] (Win95; I)
X-Accept-Language: en-GB,en,en-*
Lines: 27
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

> Can you suggest what to use instead?

	No, I reckon time() is fine. Remember that DJGPP and most other compilers of
most other languages get random numbers from a list of random numbers, you are
just roughly setting the list entry point (it might do something like use your
seed to lookup a table entry and use that entry to use a start position - I
don't think the behaviour is strictly defined).

	So, your program will give entirely different results for its entire run for
every unique second of the day it starts on. If your program is being started
more than once a second, you might consider doing something like reading the
time and getting the t-state timer value if your CPU supports it (pentium and
above) to combine. XORs are always good for that sort of thing.

	Other sources of random numbers might be to search your hard disk until you
find the first file (starting in a temp directory would be good) and read some
bytes from that, or create a pointer and read from it without allocating
memory to it (whatever you do, don't write to it though!). The possibilities
are literally endless.

	Another good idea might be for your program to read a random value on exit
and save it to load the next time it starts as a random seed. That would
probably be quite unpredictable, especially if the number of times you get
random numbers varies from execution to execution. The Linux kernel does
something similar to this.

		-Thomas

- Raw text -


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