Practice Assignment

(dice1.c)

Information

  1. Write a program that similates rolling 5 dice like in the game of Yahtzee. Yahtzee is kind of like the card game poker, but using dice instead of cards. On Linux, the game is called Kiriki.

    You will ask the user how many times to roll the 5 dice. For each roll, you will need to generate 5 random numbers from 1 to 6. You are to count how many times all of the dice are the same value (5 of a kind, a Yahtzee). After all of the rolls have been done, you will display how many times all of the dice were the same and the percentage.

    The actual probability of rolling a "5 of a kind" is 1/1296, or about 0.0007716. (The Internet is rife with all kinds of Yahtzee-related stuff.)

    Here is the output from the program run interactively:

    How many times do you want to roll the dice? (0=quit) 100
    After 100 rolls, you had 5 of a kind 0 times.
    This is a percentage of 0.00000000.
    
    How many times do you want to roll the dice? (0=quit) 1000
    After 1000 rolls, you had 5 of a kind 2 times.
    This is a percentage of 0.00200000.
    
    How many times do you want to roll the dice? (0=quit) 1000
    After 1000 rolls, you had 5 of a kind 0 times.
    This is a percentage of 0.00000000.
    
    How many times do you want to roll the dice? (0=quit) 1000
    After 1000 rolls, you had 5 of a kind 1 time.
    This is a percentage of 0.00100000.
    
    How many times do you want to roll the dice? (0=quit) 10000
    After 10000 rolls, you had 5 of a kind 8 times.
    This is a percentage of 0.00080000.
    
    How many times do you want to roll the dice? (0=quit) 20000
    After 20000 rolls, you had 5 of a kind 10 times.
    This is a percentage of 0.00050000.
    
    How many times do you want to roll the dice? (0=quit) 12345
    After 12345 rolls, you had 5 of a kind 11 times.
    This is a percentage of 0.00089105.
    
    How many times do you want to roll the dice? (0=quit) 0
    

    The program continues to run until the user types in 0 (zero) for the number of rolls.

    Questions: What do you notice as you increase the number of rolls? How large of an integer can you provide? Caution: A very large integer could cause the program to take a long time to finish. (Press Ctrl-C to stop the program, if you get tired of waiting.)

    The name of the file should be dice1.c and the command to compile it will look like this:
    gcc -Werror -Wall -Wextra -ansi -pedantic -O2 -Wno-unused-result -o dice1 dice1.c PRNG.c
    
    Files:

Notes

Mead trivia: Yahtzee was one of the first computer "games" I ever wrote. I coded in SanyoBasic on the Sanyo MBC550 (but mine had only one floppy disk drive). I had to write my own games because there was no software for this computer! I also had to upgrade the RAM from 128K to 256K (yes, that's a K) to do the 2 color 640x200 graphics on a low end video chip because I wanted to draw the dice (with pathetic animation) on the screen. I wish I still had that code so I could show students how NOT to write code. (Butt-ugly comes to mind.)