Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randrange
- from matplotlib import pyplot as plt
- import numpy as np
- import matplotlib.animation as animation
- # Function 1
- def simulate():
- # I will create a random number from 0 to 5. If it is 0 ---> win
- # I will count the times that are needed till someone wins
- r = randrange(6)
- counter = 1
- while r != 0:
- r = randrange(6)
- counter += 1
- if counter % 2 == 1:
- return "A", counter
- return "B", counter
- # Function 2
- def simulate_rounds(SIMS):
- wins_A = 0
- rounds = list()
- for SIM in range(SIMS):
- winner, counter = simulate()
- print("Round " + str(SIM + 1) + " ---> " + winner)
- if winner == "A":
- wins_A += 1
- rounds.append(counter)
- print()
- perc_A = 100 * wins_A / SIMS
- print("Simulations: " + str(SIMS))
- print("A wins: " + str(wins_A) + " (" + str(perc_A) + " %)")
- print("A wins: " + str(wins_A) + " (" + str(100 - perc_A) + " %)")
- return rounds
- def prepare_animation(bar_container):
- def animate(frame_number):
- # simulate new data coming in
- for count, rect in zip(n, bar_container.patches):
- rect.set_height(count)
- return bar_container.patches
- return animate
- # MAIN FUNCTION
- print("Roulette Original")
- print("PLayer A first shots at player B. If he shots correctly, game is over.")
- print("If player A misses, then player B shots. If he also misses, then A plays again and so on.")
- print()
- print()
- SIMS = 10**4
- rounds = simulate_rounds(SIMS)
- HIST_BINS = np.linspace(1, 100, 100)
- n, _ = np.histogram(rounds, HIST_BINS)
- plt.hist(rounds, 10)
- plt.figure()
- fig, ax = plt.subplots()
- _, _, bar_container = ax.hist(rounds, HIST_BINS, lw=1,
- ec="yellow", fc="green", alpha=0.5)
- ax.set_ylim(top=70) # set safe limit to ensure that all data is visible.
- ani = animation.FuncAnimation(fig, prepare_animation(bar_container), 50,
- repeat=False, blit=True)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement