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.
One last exercise. A good idea is to modify the above code.
> 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 >