Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import ceil
- from random import randrange
- from matplotlib import pyplot as plt
- # Function 1 - Determine the winners
- def determine_winners(num_winners, all):
- winners = list()
- for i in range(num_winners):
- r = randrange(1, all+1)
- while r in winners:
- r = randrange(1, all+1)
- winners.append(r)
- winners = sorted(winners)
- return winners
- # Function 2 - Check my bets
- def check(mine, winners):
- flag = False
- for my in mine:
- if my in winners:
- flag = True
- break
- return flag
- # Function 3 - Simulate many rounds
- def simulate(SIMS, num_winners, all, mine):
- counter = 0
- for SIM in range(SIMS):
- winners = determine_winners(num_winners, all)
- flag = check(mine, winners)
- if flag:
- counter += 1
- return counter
- # MAIN FUNCTION
- SIMS = 1000
- divisor = 5 # It gives the percentage (1/divisor) of tickets I purchased
- all_list = [10*i for i in range(1, 101)]
- num_winners_list = [1 for elem in all_list]
- num_mine_list = [ceil(elem / divisor) for elem in all_list]
- LEN = len(all_list)
- # print(all_list)
- # print(num_winners_list)
- # print(num_mine_list)
- perc_list = list()
- for i in range(LEN):
- print(60 * "*")
- all = all_list[i]
- num_winners = num_winners_list[i]
- num_mine = num_mine_list[i]
- mine = list(range(1, num_mine + 1))
- counter = simulate(SIMS, num_winners, all, mine)
- print(" We want: " + str(num_winners) + " of " + str(all) + " tickets/contestants.")
- print("I bought the first: " + str(len(mine)) + " of " + str(all) + " tickets sold.")
- percentage100 = 100 * counter / SIMS
- perc_list.append(percentage100)
- print("Simulations won = " + str(counter) + " / " + str(SIMS) + " = " + str(percentage100) + " %.")
- print(60 * "*")
- print()
- # Plotting
- average = sum(perc_list) / len(perc_list)
- print("Divisor = " + str(divisor) + " ---> Average simulations percentage of win = " + str(average) + " %.")
- mean_list = [100 / divisor for elem in all_list]
- plt.plot(all_list, perc_list)
- plt.plot(all_list, mean_list)
- plt.title("Divisor = " + str(divisor))
- plt.xlabel("Tickets")
- plt.ylabel("Percentage % of winning")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement