Last active
January 23, 2021 20:38
-
-
Save map0logo/eff3881c1de108d8486142161b96a455 to your computer and use it in GitHub Desktop.
Dice simulation to show Spyder debugging tools.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Simulating the sum of two dice. | |
Created on Fri Jan 22 19:02:16 2021 | |
@author: spyderlieber | |
""" | |
from random import randrange | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def sim_dice(faces=6): | |
""" | |
Roll and sum two dice of a given number of faces. | |
Parameters | |
---------- | |
faces : int, optional | |
Dice number of faces. The default is 6. | |
Returns | |
------- | |
int | |
Sum of two simulated dice. | |
""" | |
d1 = randrange(1, faces + 1) | |
d2 = randrange(1, faces + 1) | |
return d1 + d2 | |
def init_count(faces=6): | |
""" | |
Initialize expected probabilities and the event counter. | |
Parameters | |
---------- | |
faces : TYPE, optional | |
DESCRIPTION. The default is 6. | |
Returns | |
------- | |
expected : dict | |
Expected probabilities for each possible outcome. | |
counts : dict | |
Counter initialized at zero for each possible outcome. | |
""" | |
expected = { | |
i: (faces - abs(faces - (i - 1))) / faces ** 2 | |
for i in range(2, (faces * 2) + 1) | |
} | |
counts = {i: 0 for i in range(2, (faces * 2) + 1)} | |
return expected, counts | |
runs = 10000 | |
faces = 6 | |
expected, counts = init_count(faces) | |
for i in range(runs): | |
t = sim_dice(faces) | |
counts[t] = counts[t] + 1 | |
print("Total Simulated Expected") | |
print(" Percent Percent") | |
for i in sorted(counts.keys()): | |
print("%5d %12.2f %12.2f" % (i, counts[i] / runs * 100, expected[i] * 100)) | |
x = np.array(list(expected.keys())) | |
width = 0.35 | |
simulated = np.array(list(counts.values())) / runs * 100 | |
expected = np.array(list(expected.values())) * 100 | |
with plt.xkcd(): | |
fig, ax = plt.subplots() | |
bar1 = ax.bar(x - width / 2, simulated, width, color="blue", alpha=0.5) | |
bar2 = ax.bar(x + width / 2, expected, width, color="orange", alpha=0.5) | |
ax.legend([bar1, bar2], ["simulated", "expected"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Simulating the sum of two dice. | |
Created on Fri Jan 22 19:02:16 2021 | |
@author: spyderlieber | |
""" | |
from random import randrange | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def sim_dice(faces=6): | |
""" | |
Roll and sum two dice of a given number of faces. | |
Parameters | |
---------- | |
faces : int, optional | |
Dice number of faces. The default is 6. | |
Returns | |
------- | |
int | |
Sum of two simulated dice. | |
""" | |
d1 = randrange(1, faces) | |
d2 = randrange(1, faces) | |
return d1 + d2 | |
def init_count(faces=6): | |
""" | |
Initialize expected probabilities and the event counter. | |
Parameters | |
---------- | |
faces : TYPE, optional | |
DESCRIPTION. The default is 6. | |
Returns | |
------- | |
expected : dict | |
Expected probabilities for each possible outcome. | |
counts : dict | |
Counter initialized at zero for each possible outcome. | |
""" | |
expected = { | |
i: (faces - abs(faces - (i - 1))) / faces ** 2 | |
for i in range(2, (faces * 2) + 1) | |
} | |
counts = {i: 0 for i in range(2, (faces * 2) + 1)} | |
return expected, counts | |
runs = 10000 | |
faces = 6 | |
expected, counts = init_count(faces) | |
for i in range(runs): | |
t = sim_dice(faces) | |
counts[t] = counts[t] + 1 | |
print("Total Simulated Expected") | |
print(" Percent Percent") | |
for i in sorted(counts.keys()): | |
print("%5d %12.2f %12.2f" % (i, counts[i] / runs * 100, expected[i] * 100)) | |
x = np.array(list(expected.keys())) | |
width = 0.35 | |
simulated = np.array(list(counts.values())) / runs * 100 | |
expected = np.array(list(expected.values())) * 100 | |
with plt.xkcd(): | |
fig, ax = plt.subplots() | |
bar1 = ax.bar(x - width / 2, simulated, width, color="blue", alpha=0.5) | |
bar2 = ax.bar(x + width / 2, expected, width, color="orange", alpha=0.5) | |
ax.legend([bar1, bar2], ["simulated", "expected"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dice simulation to show Spyder debugging tools.