Advertisement
makispaiktis

Vsauce2 - This is not 50/50

Jul 13th, 2021 (edited)
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. from random import randrange, shuffle
  2. choices = ["BBB", "BBR", "BRB", "BRR", "RBB", "RBR", "RRB", "RRR"]
  3. N = len(choices)
  4.  
  5. # Function 1 - Make black/red ---_. red/black
  6. def opposite(letter):
  7.     if letter == "B":
  8.         return "R"
  9.     return "B"
  10.  
  11. # Function 2 - Find the wanted sequence
  12. def findTheWinningSequence(sequence):
  13.     if len(sequence) != 3:
  14.         print("Error while using function '" + findTheWinningSequence.__name__ + "'")
  15.         return -1000
  16.     first = opposite(sequence[1])
  17.     winningSequence = first + sequence[0:2]
  18.     return winningSequence
  19.  
  20. # Function 3 - Create a random shuffled cards deck with black and red cards
  21. def createRandomDeck():
  22.     deck1 = ["B" for i in range(26)]
  23.     deck2 = ["R" for i in range(26)]
  24.     deck1.extend(deck2)
  25.     shuffle(deck1)
  26.     shuffle(deck1)
  27.     return "".join(deck1)
  28.  
  29. # Function 4 - Calculate theoritical possibility of winning
  30. def calculatePossibility():
  31.     poss = [2, 2, 2, 2, 3, 3, 7.5, 7.5]
  32.     SUM = sum(poss)
  33.     average = SUM / len(poss)
  34.     possibility = average / (average + 1)
  35.     possibility = round(100 * possibility, 3)
  36.     return possibility          # Answer = 78.378
  37.  
  38. # Function 5 - Find the winner
  39. def findWinner():
  40.     # I will return a tuple = (winner, deck, lastIndex)
  41.     deck = createRandomDeck()
  42.     sequence = choices[randrange(N)]
  43.     winningSequence = findTheWinningSequence(sequence)
  44.     print("Deck = " + str(deck))
  45.     print("Sequence = " + str(sequence) + ", winningSequence = " + str(winningSequence))
  46.     # Throw the 1st 3 letters
  47.     table = deck[0:3]
  48.     counter = 2
  49.     # I will return 1, if 1st player wins, otherwise I will return 2
  50.     if sequence == table:
  51.         return 1, deck, counter
  52.     elif winningSequence == table:
  53.         return 2, deck, counter
  54.     # We enter the while-loop, if the first 3 letters don't match any of the 2 sequences
  55.     while table[len(table)-3:len(table)] != sequence and table[len(table)-3:len(table)] != winningSequence:
  56.         counter += 1
  57.         table = table[len(table)-2:len(table)] + deck[counter]
  58.         if sequence == table:
  59.             return 1, deck, counter
  60.         elif winningSequence == table:
  61.             return 2, deck, counter
  62.  
  63.  
  64. # Function 6 - Pretty winner
  65. def prettyWinner(winner, deck, counter):
  66.     indexesString = "Check deck in indexes " + str(counter-2) + ", " + str(counter-1) + ", " + str(counter)
  67.     print("Player " + str(winner) + " wins! " + indexesString)
  68.     if counter == 2:
  69.         print(str(deck[0:3]) + " - " + str(deck[3:]))
  70.     else:
  71.         result1 = deck[0:counter-2]
  72.         result2 = deck[counter-2:counter+1]
  73.         result3 = deck[counter+1:]
  74.         print(str(result1) + " - " + str(result2) + " - " + str(result3))
  75.     print()
  76.  
  77. # MAIN FUNCTION
  78. LIMIT = 10**4
  79. winner2Counter = 0
  80. for i in range(1, LIMIT+1):
  81.     print("**************************** Round " + str(i) + " ****************************")
  82.     winner, deck, counter = findWinner()
  83.     prettyWinner(winner, deck, counter)
  84.     if winner == 2:
  85.         winner2Counter += 1
  86.  
  87. # Now, I have to do the stats
  88. percentage = 100 * winner2Counter / LIMIT
  89. percentage = round(percentage, 3)
  90. print("Player 2 winning percentage  = " + str(percentage) + " %.")
  91. print("Expected player 2 percentage = " + str(calculatePossibility()) + " %.")
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement