Advertisement
makispaiktis

Lottery

May 13th, 2022 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. from math import ceil
  2. from random import randrange
  3. from matplotlib import pyplot as plt
  4.  
  5. # Function 1 - Determine the winners
  6. def determine_winners(num_winners, all):
  7.     winners = list()
  8.     for i in range(num_winners):
  9.         r = randrange(1, all+1)
  10.         while r in winners:
  11.             r = randrange(1, all+1)
  12.         winners.append(r)
  13.     winners = sorted(winners)
  14.     return winners
  15.  
  16.  
  17. # Function 2 - Check my bets
  18. def check(mine, winners):
  19.     flag = False
  20.     for my in mine:
  21.         if my in winners:
  22.             flag = True
  23.             break
  24.     return flag
  25.  
  26.  
  27. # Function 3 - Simulate many rounds
  28. def simulate(SIMS, num_winners, all, mine):
  29.     counter = 0
  30.     for SIM in range(SIMS):
  31.         winners = determine_winners(num_winners, all)
  32.         flag = check(mine, winners)
  33.         if flag:
  34.             counter += 1
  35.     return counter
  36.  
  37.  
  38. # MAIN FUNCTION
  39. SIMS = 1000
  40. divisor = 5                                              # It gives the percentage (1/divisor) of tickets I purchased
  41. all_list = [10*i for i in range(1, 101)]
  42. num_winners_list = [1 for elem in all_list]
  43. num_mine_list = [ceil(elem / divisor) for elem in all_list]
  44. LEN = len(all_list)
  45. # print(all_list)
  46. # print(num_winners_list)
  47. # print(num_mine_list)
  48. perc_list = list()
  49.  
  50. for i in range(LEN):
  51.     print(60 * "*")
  52.     all = all_list[i]
  53.     num_winners = num_winners_list[i]
  54.     num_mine = num_mine_list[i]
  55.     mine = list(range(1, num_mine + 1))
  56.     counter = simulate(SIMS, num_winners, all, mine)
  57.     print("           We want: " + str(num_winners) + " of " + str(all) + " tickets/contestants.")
  58.     print("I bought the first: " + str(len(mine)) + " of " + str(all) + " tickets sold.")
  59.     percentage100 = 100 * counter / SIMS
  60.     perc_list.append(percentage100)
  61.     print("Simulations won = " + str(counter) + " / " + str(SIMS) + " = " + str(percentage100) + " %.")
  62.     print(60 * "*")
  63.     print()
  64.  
  65. # Plotting
  66. average = sum(perc_list) / len(perc_list)
  67. print("Divisor = " + str(divisor) + " ---> Average simulations percentage of win = " + str(average) + " %.")
  68. mean_list = [100 / divisor for elem in all_list]
  69. plt.plot(all_list, perc_list)
  70. plt.plot(all_list, mean_list)
  71. plt.title("Divisor = " + str(divisor))
  72. plt.xlabel("Tickets")
  73. plt.ylabel("Percentage % of winning")
  74. plt.show()
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement