Question
(Python) Dice are often employed in games of chance. In this lab, we are going to simulate throwing two 6-sided dice, and adding the results,
(Python)
Dice are often employed in games of chance.
In this lab, we are going to simulate throwing two 6-sided dice, and adding the results, many, many, many times over. Since the possible values on such dice range from 1 to 6, the possible outcomes for these trials is a value from 2 to 12.
Recording Results
A data structure should be employed to hold the counts of all the trial results. In essence you need to track the number of 2's, 3's, 4's, 11's, and 12's that result from throwing two dice.
The data structure should be initialized so that all the counts start at 0.
User input
The program should begin by prompting the user to enter a random seed number (integer). This will be used to seed the random number generator.
The user should then be prompted to enter the number of trials, as a value from 1 to 360. The program should validate the input, repeatedly requesting the user for a value until it falls within the required range.
Running the Trials
A loop should be employed to run the same block of code repeatedly. This block of code does the following:
- Simulates a 6-sided dice throw (generates a number from 1..6).
- Simulates a second 6-sided dice throw (another number from 1..6).
- Adds the two results together -- this is the trial result.
- Increments the appropriate counter for that result.
Printing the Results
Once the trials have completed, and the results tabulated, the program must output a report, visualizing the results.
The report should start and end with a dashed-line. The main body of the report will be one line for each outcome (2..12), displaying the outcome number, a line of asterisks (*) equal in length to the outcome value, and the outcome value in parentheses.
An Example Run
Enter a random seed (integer): 42 Enter the number of trials (1..360): 256 ------------------------------------------------------------------ 2 : *********** (11) 3 : *************** (15) 4 : ****************** (18) 5 : *************************************** (39) 6 : ********************************* (33) 7 : ********************************************** (46) 8 : ************************** (26) 9 : ***************************** (29) 10 : ************** (14) 11 : *************** (15) 12 : ********** (10) ------------------------------------------------------------------
Hints
1) Use a while loop for the input validation.
2) Use range() in a for loop, for the given number of trials.
3) Use the randint() function to generate dice throws.
4) list * k (where k is an integer) will generate a new list consisting of k copies of list concatenated together. For example:
[1] * 4 => [1, 1, 1, 1]
5) string * k (where k is an integer) will generate a new string consisting of string duplicated k times. For example:
'x' * 4 => 'xxxx'
6) In a formatted string, the field width can be specified as a number after a colon, inside the braces. For example:
val = 99 f'[{val:4}]' => '[ 99]'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started