Skip to content

Instantly share code, notes, and snippets.

@craiga
Last active May 10, 2022 15:29
Show Gist options
  • Save craiga/977d2ae5450af2241d5e0f137938f440 to your computer and use it in GitHub Desktop.
Save craiga/977d2ae5450af2241d5e0f137938f440 to your computer and use it in GitHub Desktop.
Roll a bunch of dice a bunch of times.
"""
Roll a bunch of dice a bunch of times.
"""
import random
if __name__ == "__main__":
results = {i: 0 for i in range(5, 51)}
iterations = 0
try:
print("Rolling dice over and over. Press Ctrl+C to stop and see results.")
while True:
d4 = random.randint(1, 4)
d6 = random.randint(1, 6)
d8 = random.randint(1, 8)
d12 = random.randint(1, 12)
d20 = random.randint(1, 20)
result = sum([d4, d6, d8, d12, d20])
results[result] += 1
iterations += 1
except KeyboardInterrupt:
print()
for value, result_count in results.items():
bar = "*" * int(result_count / iterations * 2000)
print(f"{value}\t{result_count / iterations * 100:.3f}%\t{bar}")
print(f"{iterations:,} iterations")
@craiga
Copy link
Author

craiga commented May 5, 2022

Example output:

Rolling dice over and over. Press Ctrl+C to stop and see results.
^C
5	0.002%	
6	0.011%	
7	0.033%	
8	0.077%	*
9	0.149%	**
10	0.264%	*****
11	0.420%	********
12	0.633%	************
13	0.887%	*****************
14	1.194%	***********************
15	1.532%	******************************
16	1.907%	**************************************
17	2.299%	*********************************************
18	2.701%	******************************************************
19	3.096%	*************************************************************
20	3.471%	*********************************************************************
21	3.815%	****************************************************************************
22	4.111%	**********************************************************************************
23	4.374%	***************************************************************************************
24	4.589%	*******************************************************************************************
25	4.734%	**********************************************************************************************
26	4.841%	************************************************************************************************
27	4.890%	*************************************************************************************************
28	4.893%	*************************************************************************************************
29	4.835%	************************************************************************************************
30	4.737%	**********************************************************************************************
31	4.575%	*******************************************************************************************
32	4.364%	***************************************************************************************
33	4.112%	**********************************************************************************
34	3.817%	****************************************************************************
35	3.466%	*********************************************************************
36	3.090%	*************************************************************
37	2.690%	*****************************************************
38	2.301%	**********************************************
39	1.904%	**************************************
40	1.525%	******************************
41	1.186%	***********************
42	0.890%	*****************
43	0.630%	************
44	0.422%	********
45	0.265%	*****
46	0.149%	**
47	0.076%	*
48	0.032%	
49	0.011%	
50	0.002%	
13,425,203 iterations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment