To: rbjones AT therightside DOT demon DOT co DOT uk Cc: djgpp AT delorie DOT com Date: Wed, 25 Feb 1998 17:38:02 -0500 Subject: Re: Randomize In DJGPP Message-ID: <19980225.173859.19262.0.matthew.krause@juno.com> From: matthew DOT krause AT juno DOT com (Matthew R Krause) Precedence: bulk Alright, easy enough. Try this: #include void main (void) { int r; int r1; int x; x=9 srandom(time(0)&0xff); //Seed r=time(0)^random()^random();//Random r1=(r%=x)+1;//Limit printf("Random number=%d\n", r1); printf("The maxium random number could be is %d", x+1); exit(0); In other words, calling srandom starts the Random NUmber Generator, seeding it with the time. If you don't seed the RNG, then it will return the same number every time . Seeding it with the same number every time also will cause a repetative sequence of numbers (bad). So we seed it with the time. You could seed it with a number the user provides (bad), or the time/value of kbhits (a la PGP), but time is user-transparent and pretty good. The random function returns a random number from 1 to ???. The line labelled limit makes 'r1' a number between 1 and x. If for some reason, you want random floats, IMHO, you could divide to random ints by each other, do some bitshifts, divide by some power of 10 (10, 100, 10000, 10^20) whatever. Good luck and have fun. If you are really interested in how the RNG works, I suggest that you read "Numerical Recipes". It can be read online, but I forget the address (ask Alta Vista for it: Numerical NEAR Recipies). _____________________________________________________________________ You don't need to buy Internet access to use free Internet e-mail. Get completely free e-mail from Juno at http://www.juno.com Or call Juno at (800) 654-JUNO [654-5866]