Mail Archives: djgpp/1996/11/29/23:11:10
I wrote this program and it proves that it is an accurate random number generator.
<<<START RANDOM.C>>>
#include <stdio.h>
#include <stdlib.h> /* for random() */
#include <time.h> /* for time() */
#include <ctype.h> /* for tolower() */
int main( void )
{
int r,x,y,number_one,number_two,number_three,number_four,number_five;
number_one = number_two = number_three = number_four = number_five = 0;
x=0;
/* seed RNG with clock value - otherwise random() will always
return the same sequence */
srandom( (int) time( 0 ) );
printf( "Type a 'q' to move on to the next example, or press Enter to continue." );
while ( tolower( getchar( ) ) != 'q' )
{
r = random( ) % 501; /* 0 to 500 */
printf( "The random number is: %d", r );
}
printf("Hold on, this may take a little while (10 seconds on my Pentium 100) . \n");
do{
y = random( ) % 501;
if((y >= 0) && (y < 100))
number_one++;
if((y >= 100) && (y < 200))
number_two++;
if((y >= 200) && (y < 300))
number_three++;
if((y >= 300) && (y < 400))
number_four++;
if((y >= 400) && (y < 500))
number_five++;
x++;
}
while(x<10000000);
printf(" 0 to 100 = %d \n",number_one);
printf("100 to 200 = %d \n",number_two);
printf("200 to 300 = %d \n",number_three);
printf("300 to 400 = %d \n",number_four);
printf("400 to 500 = %d \n",number_five);
return 0;
}
<<<END RANDOM.C>>>
- Raw text -