delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/19/21:49:15

To: dmt AT bigfoot DOT com
Cc: djgpp AT delorie DOT com
Date: Mon, 19 Jan 1998 14:32:32 -0500
Subject: Random numbers
Message-ID: <19980119.143234.3846.2.matthew.krause@juno.com>
From: matthew DOT krause AT juno DOT com (Matthew R Krause)

Well, you might try something like this. And, if you are feeling really
enegetic, you can even convert it into a header file (something like
random.h). Anyway

#include <stdio.h>
#include <time.h>
void main (void)
{
int r1;
int r2;
/*Starts randomization*/
srandom(time(0)&0xff);	 /* This seeds the Random number generator
with 			something derrived from the system clock.
This way, the 			random number changes.
Unfortunatly, because the 			time changes only 
once/second, if you put this program 			into a loop
(ie while(1)),  when you finally break it, your
			screen will be filled with the same number. Other
ways 			to seed it include time between kbhits or
the value of a 			few getchars(), but time is
probobly the easiest.*/

r1=time(0)^random()^random(); 
			/* This makes Random equal to the value of time
raised 			to the power of a random number rasied to
the power of 			a random number. This gives you one
random number*/

r2=r1%=16;		
			/* Since 'r1' is probobly a __tad__ bit too big
for any use 			in your program, we limit it to a
value using this line. 			Since I was using this
example to change the color of 			text in my program
I set it to sixteen (cause there are 16 			colors).
What actually happens is that r1 is divided by 16
			and remainder will be x over sixteen. The modulus
(%) 			function returns that x.*/
printf("%d\n", r1);		/* Self Explainitory*/
printf("%d\n", r2);		/*See above*/
exit(0);			/*See above :>) */
}

I'm sorry if this sounds somewhat patronizing or obvious, but from your
post I was having trouble getting a feel for your ability level. Good
luck.


Matthew Krause
Orange, CT, USA
**If two wrongs don't make a right, try three**

- Raw text -


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