Illustrating the probability distribution.

★★★

If a computer's random number generator is providing an accurate distribution of numbers, when you simulate the roll of two dice and record the sum, you should achieve a triangular curve in the result of a thousand rolls. This program illustrates the output of the random number generating Mersenne Twister algorithm used by the random library in Python.

Distribution of two dice

Make

Write a program that outputs the number of 2’s, 3’s, 4’s etc. that are rolled by adding two random numbers between one and six. The number of rolls to simulate is input by the user.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called distribution that:

  1. Takes one argument, the number of two dice rolls to simulate.
  2. Rolls two dice, sums them together and adds the result to a tally. E.g. If a four and three are rolled the sum is seven, so the tally for the number seven is increased by one.
  3. Outputs the results for the ten possible rolls, two to twelve.

Complete the main program so that:

  1. The user can input how many rolls to simulate.
  2. It calls the distribution subprogram.

Typical inputs and outputs from the program would be:

2 : 34
3 : 61
4 : 82
5 : 131
6 : 147
7 : 168
8 : 130
9 : 101
10 : 79
11 : 44
12 : 23
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
# Distribution of two dice program

# -------------------------
# Import libraries
# -------------------------
---
import random
---


# -------------------------
# Subprograms
# -------------------------
---
def distribution(rolls):
---
    # Initialise dice and results
    dice = [0, 0]
    result = [0 for roll in range(13)]
    random.seed()
---
    
    # Simulate dice rolls
    for counter in range(rolls):
---
        dice[0] = random.randint(1, 6)
        dice[1] = random.randint(1, 6)
---
        sum = dice[0] + dice[1]
---
        result[sum] = result[sum] + 1
---
        
    # Output result
    for roll in range(2, len(result)):
---
        print(roll, ":", result[roll])
---
        
        
# -------------------------
# Main program
# -------------------------
---
rolls = int(input("How many rolls to simulate? :"))
---
distribution(rolls)