From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: rand(), random() !!!!!!!!!!!!!!!!!!!!!!!?????? Date: Thu, 02 Jul 1998 22:43:40 -0400 Organization: Two pounds of chaos and a pinch of salt. Lines: 49 Message-ID: <359C455C.15852BA0@cs.com> References: <1998070219214100 DOT PAA22119 AT ladder03 DOT news DOT aol DOT com> NNTP-Posting-Host: ppp114.cs.net 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 MalcolmJ7 wrote: > > how do i randomize these functions? > every time i use them they give me the same numbers, in the same order. > when I use srandom() this, still gives me same numbers, in the same order. > > isn't there a randomize() statement, like in turbo c? When you use srandom() or srand(), you have to give them a value to use as the seed. If you give them the same value every time, they will of course generate the same sequence of random numbers. The most commonly used RNG seed is the system clock, because it is guaranteed to be different each time you run your program (within a granularity of 1 second or so). Sample code: #include #include #include int main( void ) { int i; srandom( (int) time( NULL ) ); for ( i = 0; i < 100; i++ ) printf( "%3d ", random() % 100 + 1 ); printf( "\n" ); return 0; } The srandom( (int) time( NULL ) ) statement is identical in meaning to Turbo C's randomize(). FYI, srand() is used to seed rand(), and srandom() seeds random(). rand() and random() are completely independent of each other. (Documentation for srand() was omitted from the libc docs of v2.01 by mistake.) hth! -- --------------------------------------------------------------------- | John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com | | Proud owner of what might one | http://www.cs.com/fighteer/ | | day be a spectacular MUD... | ICQ UIN#: 7406319 | | Plan: To make Bill Gates suffer | HEAT User ID: Fighteer | ---------------------------------------------------------------------