-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_rolls.py
More file actions
42 lines (35 loc) · 915 Bytes
/
Copy pathrandom_rolls.py
File metadata and controls
42 lines (35 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Simulate 6000 rolls of a die (1-6)
"""
import random
import statistics
def roll_die(num):
"""
Random roll of a die
:param num: number of rolls
:return: a list of frequencies.
Index 0 maps to 1
Index 1 maps to 2
etc
"""
freq = [0] * 6 # initial val to 0
for roll in range(num):
n = random.randrange(1, 7)
freq[n - 1] += 1
# print(random.randrange(1,7))
return freq
def main():
"""
Test function
:return:
"""
num = int(input("How many times you need to roll: "))
result = roll_die(num)
for roll, total in enumerate(result):
print("Total rolls of {} = {}".format(roll + 1, total))
print("Average = {}".format(sum(result)/len(result)))
print("Mean = {}".format(statistics.mean(result)))
print("Median = {}".format(statistics.median(result)))
if __name__ == '__main__':
main()
exit(0)