Advertisement
makispaiktis

Roulette Original - Animation

May 28th, 2022 (edited)
1,626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. from random import randrange
  2. from matplotlib import pyplot as plt
  3. import numpy as np
  4. import matplotlib.animation as animation
  5.  
  6. # Function 1
  7. def simulate():
  8.     # I will create a random number from 0 to 5. If it is 0 ---> win
  9.     # I will count the times that are needed till someone wins
  10.     r = randrange(6)
  11.     counter = 1
  12.     while r != 0:
  13.         r = randrange(6)
  14.         counter += 1
  15.     if counter % 2 == 1:
  16.         return "A", counter
  17.     return "B", counter
  18.  
  19. # Function 2
  20. def simulate_rounds(SIMS):
  21.     wins_A = 0
  22.     rounds = list()
  23.     for SIM in range(SIMS):
  24.         winner, counter = simulate()
  25.         print("Round " + str(SIM + 1) + " ---> " + winner)
  26.         if winner == "A":
  27.             wins_A += 1
  28.         rounds.append(counter)
  29.  
  30.     print()
  31.     perc_A = 100 * wins_A / SIMS
  32.     print("Simulations: " + str(SIMS))
  33.     print("A wins: " + str(wins_A) + " (" + str(perc_A) + " %)")
  34.     print("A wins: " + str(wins_A) + " (" + str(100 - perc_A) + " %)")
  35.     return rounds
  36.  
  37.  
  38. def prepare_animation(bar_container):
  39.  
  40.     def animate(frame_number):
  41.         # simulate new data coming in
  42.         for count, rect in zip(n, bar_container.patches):
  43.             rect.set_height(count)
  44.         return bar_container.patches
  45.     return animate
  46.  
  47.  
  48.  
  49. # MAIN FUNCTION
  50. print("Roulette Original")
  51. print("PLayer A first shots at player B. If he shots correctly, game is over.")
  52. print("If player A misses, then player B shots. If he also misses, then A plays again and so on.")
  53. print()
  54. print()
  55. SIMS = 10**4
  56. rounds = simulate_rounds(SIMS)
  57. HIST_BINS = np.linspace(1, 100, 100)
  58. n, _ = np.histogram(rounds, HIST_BINS)
  59. plt.hist(rounds, 10)
  60. plt.figure()
  61. fig, ax = plt.subplots()
  62. _, _, bar_container = ax.hist(rounds, HIST_BINS, lw=1,
  63.                               ec="yellow", fc="green", alpha=0.5)
  64. ax.set_ylim(top=70)  # set safe limit to ensure that all data is visible.
  65.  
  66. ani = animation.FuncAnimation(fig, prepare_animation(bar_container), 50,
  67.                               repeat=False, blit=True)
  68. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement