-
-
Save eagskunst/0bec6f774f7e1fbba58d00ef854c8881 to your computer and use it in GitHub Desktop.
Dices rolls and probabilities
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
import random | |
from bokeh.plotting import figure, show, output_file, save | |
dice_seq = [1, 2, 3, 4, 5, 6] | |
def roll_dices(rolls_n): | |
rolls_secuence = [] | |
for _ in range(rolls_n): | |
roll = random.choice(dice_seq) | |
roll2 = random.choice(dice_seq) | |
rolls_secuence.append(roll + roll2) | |
return rolls_secuence | |
def plot_values(x, y, desired_num): | |
plot = figure(title='Rolls probabilities', x_axis_label=f'Probability of {desired_num} appearing at least once', y_axis_label='Number of dice throws') | |
plot.line(x, y, legend_label='Probabilities') | |
output_file('two_dice_probs.html') | |
save(plot) | |
def main(rolls_n, simulations, desired_num): | |
rolls = [] | |
probs = [] | |
for roll_n in range(1, rolls_n + 1): | |
for _ in range(simulations): | |
rolls_secuence = roll_dices(roll_n) | |
rolls.append(rolls_secuence) | |
desired_rolls = [1 for roll in rolls if desired_num in roll] | |
prob = sum(desired_rolls) / simulations | |
probs.append(prob) | |
rolls.clear() | |
plot_values(range(1, rolls_n + 1), probs, desired_num) | |
if __name__ == '__main__': | |
desired_num = int(input('Enter the number to study (2-12): ')) | |
rolls_n = int(input('Enter the max rolls: ')) | |
simulations = int(input('Enter the number of simulations for each roll: ')) | |
main(rolls_n, simulations, desired_num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment