Skip to content

Instantly share code, notes, and snippets.

@ericsong
Created November 13, 2015 11:08
Show Gist options
  • Save ericsong/399a0999d95091c8754a to your computer and use it in GitHub Desktop.
Save ericsong/399a0999d95091c8754a to your computer and use it in GitHub Desktop.
Reno Jackson Probabilities Simulation
import random, time
RUNS_PER_DECK = 1000000 #million
def generateDeck(numDoubles):
""" Generate a random array of 30 numbers representing unique cards """
numUnique = 30 - (numDoubles*2) - 1
deck = [0]
cardId = 1
for i in range(numDoubles):
deck.append(cardId)
deck.append(cardId)
cardId = cardId + 1
for i in range(numUnique):
deck.append(cardId)
cardId = cardId + 1
random.shuffle(deck)
return deck
def getActivationIndex(deck):
""" Returns index where the deck is entirely composed of unique cards """
visited = []
activationIndex = len(deck) - 1
for i in range(len(deck)-1, -1, -1):
e = deck[i]
if e in visited:
return activationIndex
else:
visited.append(e)
activationIndex = activationIndex - 1
return 0
def drawsForReno(deck):
""" Returns how many card draws would be needed to activate Reno """
renoIndex = deck.index(0)
activationIndex = getActivationIndex(deck)
return max(renoIndex, activationIndex)
def testTypeOfDeck(numDoubles, runs):
start = time.time()
drawSum = 0
for i in range(runs):
drawSum = drawSum + drawsForReno(generateDeck(numDoubles))
end = time.time()
averageDraws = drawSum / runs
timeTaken = end - start
return averageDraws, timeTaken
def printResults(numDoubles, draws, timeTaken):
#print("Number of doubles: " + str(numDoubles) + "\t Average number of draws: " + str(draws) + "\t Time Taken: " + str(timeTaken) + " seconds")
print(str(numDoubles) + "\t" + str(draws))
for i in range(15):
numDoubles = i
draws, timeTaken = testTypeOfDeck(numDoubles, RUNS_PER_DECK)
printResults(numDoubles, draws, timeTaken)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment