Random Numbers again

Chapter: Random Numbers again

Last week we saw code for pseudorandom.c

If you add

main(argc, argv) 
     int argc;
     char *argv[];
{
/* If the optional argument is on the commandline use it to reset the seed */
  if (argc > 1) set_seed(atoi(argv[1]));  
  int top = 100;                       /* default is ints from 0 - 99 */
  if (argc > 2) top = atoi(argv[2]);   /* but can be adjusted by second arg */
  int i;
  char continu = 'g';
  while (continu != 'q') {
    for (i = 0; i < 10; i++) {
      printf("%d ",  (int) prandom_range(0.0,top*1.0));
    }
    printf("\n");
    printf("Hit q to quit, ENTER to continue > ");
    continu = getchar();
  }
  getchar(); /* eat the CR/LF */
}
and compile via gcc -o pseudorandom pseuedorandom.c and run it by simply typing pseudorandom you will get a sequence of pseudo-random numbers in the range 0-99. It will always be exactly the same sequence because the initial seed is fixed. By using argc and argv and atoi() you can select a new intial seed for each run. Typing pseudorandom 19071961 you will get a different sequence.

Q. 1
What is the point of this?


One last exercise. A good idea is to modify the above code.


Exercise 1

Write a program to keep dealing cards from a standard deck with replacement. This means that each of the 54 cards is equally likely to be selected each time even if it was just drawn. Your program should behave like this:
> cards 47
ace of clubs
ENTER to continue, q to quit > 
Joker
ENTER to continue, q to quit > 
deuce of diamonds
ENTER to continue, q to quit > 
ten of spades
ENTER to continue, q to quit > 
ace of hearts
ENTER to continue, q to quit > 
knave of spades
ENTER to continue, q to quit > 
queen of clubs
ENTER to continue, q to quit > 
king of hearts
ENTER to continue, q to quit > 
7 of diamonds
ENTER to continue, q to quit > q
> 



rhyspj@gwu.edu