Visualize
main.py
import random DETERMINISTIC_MODE = True ITERATIONS_IN_RANDOM_MODE = 100000 length = 6 tailProbabilityNumerator = 2 headProbabilityNumerator = 3 probabilityDenominator = headProbabilityNumerator + tailProbabilityNumerator if DETERMINISTIC_MODE: numIterations = probabilityDenominator ** length else: numIterations = ITERATIONS_IN_RANDOM_MODE def playGame(table): aliceCounter = bobCounter = 0 aliceIndex = bobIndex = 1 while aliceIndex <= length: if table[aliceIndex] < headProbabilityNumerator: aliceCounter += 1 if table[bobIndex] < headProbabilityNumerator: bobCounter += 1 if aliceCounter == 2 and bobCounter < 2: return "Alice" if bobCounter == 2 and aliceCounter < 2: return "Bob" if aliceCounter == 2 and bobCounter == 2: return "Tie" aliceIndex += 1 bobIndex += 2 if bobIndex > length: bobIndex = 2 return "Tie" # Wasting an entry at the beginning to follow the same 1-indexing convention used in the exposition table = [None] * (length + 1) aliceWins = bobWins = playedGames = 0 while playedGames < numIterations: index = 1 if DETERMINISTIC_MODE: # The digits of m in base probabilityDenominator are used as the values of our table m = playedGames while(index <= length): if DETERMINISTIC_MODE: table[index] = m % probabilityDenominator m = m // probabilityDenominator else: table[index] = random.randint(0, probabilityDenominator - 1) index += 1 returnValue = playGame(table) if returnValue == "Alice": aliceWins += 1 if returnValue == "Bob": bobWins += 1 playedGames += 1 if DETERMINISTIC_MODE: print (str(aliceWins) + "/" + str(numIterations), str(bobWins) + "/" + str(numIterations)) else: print(aliceWins/numIterations, bobWins/numIterations)
Output