Appendix I — Homework: Random Numbers
Submit this homework via Dropbox. For the deadline please consult the schedule posted on Blackboard.
- Python-scripts are submitted via Dropbox in one file called:
homework8.py
I.1 Exercise 1
Flip a coin N times. Make a program that simulates flipping a coin N times. Print out "tail" or "head" for each flip and let the program count the number of heads. Hint: Use r = np.random.rand() and define head
as r <= 0.5 or draw an integer between {1, 2} and define head when r is 1.
I.2 Exercise 2
Compute a probability. What is the probability of getting a number between 0.5 and 0.6 when drawing uniformly distributed random numbers from the interval [0, 1]?
To answer this question empirically, let a program draw N such random numbers using Python's standard random module. Count how many of them, M , fall in the interval (0.5, 0.6), and compute the probability as M/N .
Hint: Run the program four times with N = 100. The tricky part is to count how many of the 100 random numbers fall in the interval (0.5, 0.6). Run the program again for N = 1000 and see whether the relative frequency changes a lot. Finally run the program with N = 10,000.
I.3 Exercise 3
Probabilities of rolling dice. Simulate the rolling of a die a thousand times. How many times does the number 6 come up?
I.4 Exercise 4: Harder question
Decide if a dice game is fair. Somebody suggests the following game. You pay 1 dollar and are allowed to throw four dice. If the sum of the eyes on the dice is less than 9, you win 10 dollars, otherwise you lose your investment. Should you play this game?
Answer the question by making a program that simulates the game 1000 times. Count how many times, out of the 1000, you would win this game in which case you'd win 10 bucks. Compare these winnings to your investment of 1000 dollars. Do you come out ahead?