Week 6: Exercise Solutions

Exercise 6.1

import random
random.seed()

NO_OF_TRIALS = 1000000

frequency = [0] * 13

for i in range(NO_OF_TRIALS):
    dice_score = random.randrange(1, 7) + random.randrange(1, 7)
    frequency[dice_score] = frequency[dice_score] + 1

print("score\trelative frequency")
print("-" * 26)
for i in range(2, 13):
    print(i, "*" * int(100*frequency[i] / NO_OF_TRIALS), sep="\t")

With two dice there are 36 possible outcomes. We can represent an outcome by an ordered pair (a, b) where a is the score on the first dice and b is the score on the second.

Exercise 6.2

import math

NO_OF_INTERVALS = 40

for i in range(NO_OF_INTERVALS + 1):
    x = 2 * math.pi * i / NO_OF_INTERVALS
    print(" " * int(30 * (1 + math.sin(x))) + "*")