Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q 3 : Throw Dice - 3 0 pts Consider that you are throwing a pair of dice. A single die yields values between 1

Q3: Throw Dice -30 pts
Consider that you are throwing a pair of dice. A single die yields values between 1 and 6
(inclusive), so a pair of dice yields a total between 2 and 12. However, the probabilities of the
total being 2,3,...,12 are not equal. You will have a total of 12 with probability 1/36(only
possibility is 6+6=12), but you will have a total of 7 with probability 6/36 since there are multiple
outcomes that cause a total of 7(dice outcomes: 1-6,2-5,3-4,4-3,5-2,6-1).
Open ThrowDice.py that is given to you. Here, you will see that we gave you the function to
throw a *single* die: throw_single_die. Do not modify this function. Use this function when
throwing a pair of dice. Solve the following parts.
Part A (10 pts): Implement the function record_dice_throws(throws).
The input parameter throws denotes how many times the pair of dice is thrown.
Your function should return a dictionary in which the keys of the dictionary are tuples
(first_die, second_die) and the values are the number of times the corresponding dice
outcome is observed.
For example, when throws=10000, the return value may look like this:
This means the outcome (1,1) was observed 260 times, the outcome (1,2) was observed
255 times, ..., and the outcome (6,6) was observed 256 times.
Part B (10 pts): Implement the function calculate_frequencies(throws_dict) such that:
The input parameter throws_dict is the dictionary created in Part A.
The function returns a list of tuples, each tuple consists of two elements:
The first element is the total of two dice
The second element is the ratio of cases in which that total was observed
Continuing from the above example, the return value of this function looks like this:
This means the total=2 was observed in 2.6% of the dice throws, total=3 was observed in
5.35% of the dice throws, ..., total=7 was observed in 16.82% of the dice throws, ..., and
total=12 was observed in 2.56% of the dice throws.
Since the minimum total is 2(because 1+1=2) and the maximum total is 12(because 6+6=12),
there are guaranteed to be 11 tuples in the returned list.
Hint: Sum of the normalized frequencies in this list should add up to 1. You can use this fact
to check the correctness of your results.
Part C (10 pts): Implement the function write_dict_to_file(throws_dict) to write the above
created dictionary to a file.
The input parameter throws_dict is the dictionary created in Part A.
Your function should create and write the above dictionary to a file called
diceresults.txt in the following format:
The contents of diceresults.txt is a 6x6 matrix corresponding to the counts of:
(1,1),(1,2),...,(1,6) in the first row separated by commas
(2,1),(2,2),...,(2,6) in the second row separated by commas
...
(6,1),(6,2),...,(6,6) in the sixth row separated by commas

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

978-0764535376

More Books

Students also viewed these Databases questions

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago