www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/25/18:34:57

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)

Alright, easy enough. Try this:

#include <stdio.h>
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]

- Raw text -


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