From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: What does "srand" and "srandom" do? Date: Sat, 07 Mar 1998 16:34:28 -0500 Organization: Two pounds of chaos and a pinch of salt. Lines: 66 Message-ID: <3501BD64.48C@cs.com> References: <34FFC3E3 DOT 8465FB75 AT mail DOT hitel DOT net> NNTP-Posting-Host: ppp233.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Ahn Ki-yung wrote: > > I wonder what does these libc functions do ? > In the libc reference info file there are 3 random functions. > They are rand, random, srandom. > The function "rand(unsigned int)" is not found but there is a prototype > in "stdlib.h" There are actually four related functions: rand(), srand(), random(), and srandom(), all declared in , but the documentation for srand() was omitted from DJGPP v2.01 by mistake. These functions all deal with pseudo-random numbers, an essential feature of any program that depends on "chance," "randomness," "free will," or any similar concept. Games, simulations, and many other types of programs absolutely depend on having access to random numbers. DJGPP has two pseudo-random number generators: rand() and random(). Both of these make use of a number called a "seed", which is used to determine the next pseudo-random number. If you don't initialize this seed value, it starts at zero, and therefore the sequence of pseudo-random numbers will be the same each time you run your program. To initalize the seed, you must call the srand() or srandom() functions, respectively, each of which sets the seed of the corresponding pseudo-random number generator. This process is called "seeding." The biggest problem is picking the value to use as the seed. Obviously, using the same number will result in the same sequence of pseudo-random numbers, so most programmers use a value that is virtually guaranteed to be unique each time the program is run: the system clock. The easiest way to retrieve this value is with the time() library function, declared in . Example code: #include #include #include int main( void ) { srandom( (int) time( NULL ) ); printf( "Random value from 1 to 100: %d\n", random() % 100 + 1 ); return 0; } srand() returns a value from 0 to RAND_MAX, defined in . random() returns a value from 0 to MAXINT. You must clip these values to the desired range before using them. Finally, some notes. The term "pseudo-random" is used because these are not true random numbers. The period of most RNGs (i.e., the number of iterations before the sequence is repeated) is usually the same as their maximum range (i.e., RAND_MAX or MAXINT). Also, not all random number generators are equal: the DJGPP rand() tends to be non-random in its low order bits. random() is a better RNG, but takes more cycles to run. Many programmers recommend implementing your own custom random number generators from books of algorithms such as Knuth, Volume 2. Hope this helps! -- --------------------------------------------------------------------- | John M. Aldrich | "Autocracy is based on the assumption| | aka Fighteer I | that one man is wiser than a million | | mailto:fighteer AT cs DOT com | men. Let's play that over again, | | http://www.cs.com/fighteer | too. Who decides?" - Lazarus Long | ---------------------------------------------------------------------