Information
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.
The name of the file should be dice1.c and the command to compile it will look like this: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.)
Files:gcc -Werror -Wall -Wextra -ansi -pedantic -O2 -Wno-unused-result -o dice1 dice1.c PRNG.c
Notes
Also, the PRNG is already seeded for you in the partial dice1.c file above. Don't change it.value = RandomInt(1, 6);
and then diff with the provided output.dice1 < input.txt > myoutput.txt
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.)