Random Number Generation

 

rand function

 

  • include<stdlib.h>

  • Returns "random" number between 0 and RAND_MAX (at least 32767)

  • i = rand();
  • Pseudorandom

  • Preset sequence of "random" numbers

  • Same sequence for every function call

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int i;
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}

 

(1) Scaling

 

  • To get a random number between 1 and n

  • 1 + ( rand() % n )

    • rand() % n returns a number between 0 and n - 1

    • Add 1 to make random number between 1 and n

  • 1 + ( rand() % 6)

  • number between 1 and 6

Shifted, scaled integers produced by 1 + rand() % 6

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;

for ( i = 1; i <= 10; i++ )
printf( "%6d", 1 + ( rand() % 6 ) );


return 0;
}

 

(2) Randomize

 

Executing the previous program produces the same sequence .How can these be random numbers ? This repeatability is an important characteristic of function rand .When debugging a program , this repeatability is essential for proving that corrections to a program work properly.

Once a program has thoroughly debugged , it can be conditioned to produce a different sequence of random numbers for each execution .This is called randomizing and it is accomplished with the standard library function srand. Function srand takes an unsigned integer argument and seeds function rand to produce a different sequence of random numbers for each execution of the program.

 

srand function

 Randomizing ways

randomize(): Initializes random number generator

srand( seed );

srand( time(NULL) );

 

#include <stdlib.h>
#include <stdio.h>

void main()
{
int i;
randomize();

for ( i = 1; i <= 10; i++ )
printf( "%5d", 1 +( rand() % 100 ) );

}

#include <stdlib.h>
#include <stdio.h>

void main()
{
int i;
unsigned seed;

printf( "Enter seed: " );
scanf( "%u", &seed );
srand( seed );

for ( i = 1; i <= 10; i++ )
printf( "%5d", 1 + ( rand() % 100 ) );

}

#include <stdlib.h>
#include <stdio.h>

void main()
{
int i;

srand( time(NULL) );

for ( i = 1; i <= 10; i++ )
printf( "%5d", 1 + ( rand() % 100 ) );

}

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
time_t t;
srand((unsigned) time(&t));

printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}

 

 Roll a six-sided die 6000 times

#include <stdio.h>
#include <stdlib.h>

int main()
{
int face, roll, frequency1 = 0, frequency2 = 0,
frequency3 = 0, frequency4 = 0,
frequency5 = 0, frequency6 = 0;

for ( roll = 1; roll <= 6000; roll++ ) {
face = 1 + rand() % 6;

switch ( face ) {
case 1:
++frequency1;
break;
case 2:
++frequency2;
break;
case 3:
++frequency3;
break;
case 4:
++frequency4;
break;
case 5:
++frequency5;
break;
case 6:
++frequency6;
break;
}
}

printf( "%s%13s\n", "Face", "Frequency" );
printf( " 1%13d\n", frequency1 );
printf( " 2%13d\n", frequency2 );
printf( " 3%13d\n", frequency3 );
printf( " 4%13d\n", frequency4 );
printf( " 5%13d\n", frequency5 );
printf( " 6%13d\n", frequency6 );
return 0;
}
 

 Craps simulator

   Rules:

  1. Roll two dice

  2. 7 or 11 on first throw, player wins

  3. 2, 3, or 12 on first throw, player loses

  4. 4, 5, 6, 8, 9, 10 - value becomes player's "point"

  5. Player must roll his point before rolling 7 to win

 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int rollDice( void );

int main()
{
int gameStatus, sum, myPoint;

srand( time( NULL ) );
sum = rollDice( ); /* first roll of the dice */

switch( sum ) {
case 7: case 11: /* win on first roll */
gameStatus = 1;
break;
case 2: case 3: case 12: /* lose on first roll */
gameStatus = 2;
break;
default: /* remember point */
gameStatus = 0;
myPoint = sum;
printf( "Point is %d\n", myPoint );
break;
}

while ( gameStatus == 0 ) { /* keep rolling */
sum = rollDice( );

if ( sum == myPoint ) /* win by making point */
gameStatus = 1;
else
if ( sum == 7 ) /* lose by rolling 7 */
gameStatus = 2;
}

if ( gameStatus == 1 )
printf( "Player wins\n" );
else
printf( "Player loses\n" );

return 0;
}

int rollDice( void )
{
int die1, die2, workSum;

die1 = 1 + ( rand() % 6 );
die2 = 1 + ( rand() % 6 );
workSum = die1 + die2;
printf( "Player rolled %d + %d = %d\n", die1, die2, workSum );
return workSum;
}