- Exactly One Ace Each

from random import randint

**#function that returns the selected card chosen at random**

def drawCard(deck):

selection = randint(0, len(deck) – 1)

card = deck[selection]

deck.pop(selection)

if(len(deck) == 0):

empty = True

else:

empty = False

return deck, card, empty

**#function that renews the deck**

def renewDeck():

deck = [‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’, ‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’, ‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’, ‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’]

return deck

**#function that determines the average**

def Average(count, BIG_NUM):

return count/float(BIG_NUM)

print ‘Enter the number of trials to run: ‘

BIG_NUM = raw_input()

BIG_NUM = int(BIG_NUM)

deck = [‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’, ‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’, ‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’, ‘A’, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘J’, ‘Q’, ‘K’]

players = [0, 1, 2, 3]

ExactlyOneEach = 0

empty = False

i = 0

while i < BIG_NUM:

playerAces = [False, False, False, False]

while empty != True:

for index in players:

deck, selectedCard, empty = drawCard(deck)

if selectedCard == ‘A’:

playerAces[index] = True

if playerAces[0] == True and playerAces[1] == True and playerAces[2] == True and playerAces[3] == True:

ExactlyOneEach = ExactlyOneEach + 1

deck = renewDeck()

empty = False

i = i + 1

print ‘Percentage of times that each player had exactly one ace each: ‘, 100*Average(ExactlyOneEach, BIG_NUM)

- No Whammy!

from random import randint

**#function to randomly choose a pre-determined number of balls from the bag**

def pullOutBalls(numRemoved):

balls = [‘gold’, ‘gold’, ‘black’, ‘black’, ‘black’, ‘black’, ‘black’,’black’,’black’,’whammy’]

ballsChosen = []

i = 0

while i < numRemoved:

indexRemoved = randint(1, len(balls)) – 1

ballRemoved = balls[indexRemoved]

ballsChosen.append(ballRemoved)

balls.pop(indexRemoved)

i = i + 1

return ballsChosen

**#function to determine whether the dream vacation was won or not**

def winOrLose(ballsChosen):

wins = 0

if ballsChosen.count(‘gold’) >= 1 and ballsChosen.count(‘whammy’) == 0:

wins = wins + 1

return wins

** ****#function that determines the number of times we won when pulling out 2 through 5 balls**

def winsForEachOption():

options= [2,3,4,5]

winsList= []

for numRemoved in options:

ballsChosen = pullOutBalls(numRemoved)

wins = winOrLose(ballsChosen)

winsList.append(wins)

return winsList

**#function that determines which option (pulling 2, 3, 4, or 5 balls) gives us the highest probability**

def bestOdds(two, three, four, five):

winningList = [two, three, four, five]

best = winningList[0]

for item in winningList:

if item > best:

best = item

return best, winningList.index(best) + 2

**#function that determines the average**

def Average(count, BIG_NUM):

return count/float(BIG_NUM)

print ‘Enter the number of trials to run: ‘

BIG_NUM = raw_input()

BIG_NUM = int(BIG_NUM)

i = 0

twoWins = 0

threeWins = 0

fourWins = 0

fiveWins = 0

while i < BIG_NUM:

winsList = winsForEachOption()

twoWins = twoWins + winsList[0]

threeWins = threeWins + winsList[1]

fourWins = fourWins + winsList[2]

fiveWins = fiveWins + winsList[3]

i = i + 1

winner, numDrawn = bestOdds(twoWins, threeWins, fourWins, fiveWins)

print ‘Average for two balls = ‘, 100*Average(twoWins, BIG_NUM)

print ‘Average for three balls = ‘, 100*Average(threeWins, BIG_NUM)

print ‘Average for four balls = ‘, 100*Average(fourWins, BIG_NUM)

print ‘Average for five balls = ‘, 100*Average(fiveWins, BIG_NUM)

print ‘For this simulation, the highest success rate was removing %i balls from the bag.’ %(numDrawn)

- Casino Chip Conundrum

from random import randint

**#function to reset the three bags**

def resetGame():

global bag1, bag2, bag3, bags

bag1 = [‘white’,’white’]

bag2 = [‘black’,’white’]

bag3 = [‘black’,’black’]

bags = [bag1, bag2, bag3]

**#function to choose the remaining chip from the same bag**

def StayAndPlay(chosenBag, sampleChip):

index = chosenBag.index(sampleChip)

newBag = chosenBag.pop(index)

lastChip = chosenBag[0]

return lastChip

**#function to choose a chip from a new bag**

def DitchToSwitch(chosenBag, bags):

index = bags.index(chosenBag)

bags.pop(index)

r = randint(0,1)

newBag = bags[r]

r2 = randint(0,1)

lastChip = newBag[r2]

return lastChip

**#function that determines the average**

def Average(count, BIG_NUM):

return count/float(BIG_NUM)

**#————————————-**

print ‘Enter the number of trials to run: ‘

BIG_NUM = raw_input()

BIG_NUM = int(BIG_NUM)

i = 0

**#values to keep count of victories**

oneWay = 0 **#if sample=black, stay. if sample=white, switch**

anotherWay = 0 **#if sample=white, stay. if sample=black, switch**

alwaysStay = 0

alwaysSwitch = 0

firstTry = 0

while i < BIG_NUM:

resetGame()

coinFlip = randint(0,2) **#with 3 sides! (0,1,2)**

bagChoice = bags[coinFlip]

coinToss = randint(0,1) **#two sides this time**

sampleChip = bagChoice[coinToss]

if sampleChip == ‘white’:

anotherChip = StayAndPlay(bagChoice, sampleChip)

resetGame()

**# we have to reset the game because we use pop()**

bagChoice = bags[coinFlip]

sampleChip = bagChoice[coinToss]

oneChip = DitchToSwitch(bagChoice, bags)

if anotherChip == ‘black’:

anotherWay +=1

alwaysStay +=1

if oneChip == ‘black’:

oneWay +=1

alwaysSwitch +=1

elif sampleChip == ‘black’:

firstTry += 1

oneChip = StayAndPlay(bagChoice, sampleChip)

resetGame()

bagChoice = bags[coinFlip]

sampleChip = bagChoice[coinToss]

anotherChip = DitchToSwitch(bagChoice, bags)

if oneChip == ‘black’:

oneWay += 1

alwaysStay +=1

if anotherChip == ‘black’:

anotherWay += 1

alwaysSwitch +=1

i = i + 1

print ‘percent wins one way: ‘, 100*Average(oneWay, BIG_NUM)

print ‘percent wins another way: ‘, 100*Average(anotherWay, BIG_NUM)

print ‘percent first try: ‘, 100*Average(firstTry, BIG_NUM)

print ‘percent wins, always stay: ‘, 100*Average(alwaysStay, BIG_NUM)

print ‘percent wins, always switch: ‘, 100*Average(alwaysSwitch, BIG_NUM)

- The Best Bet

from random import randint

**#create a class for the dice**

class Dice:

def __init__(self, sides):

self.sides = [i for i in range(1, sides+1)]

def makeRoll(self):

sideChoice = randint(1,6)

return sideChoice

die = Dice(6)

**#———-for six dice————————–**

print ‘Enter the amount of trials to run: ‘

BIG_NUM = raw_input()

BIG_NUM = int(BIG_NUM)

j = 0

sixCount = []

while j < BIG_NUM:

i = 0

choices = []

while i <6:

sideChoice = die.makeRoll()

choices.append(sideChoice)

i = i + 1

sixCount.append(choices.count(6))

j = j + 1

average = 1- sixCount.count(0)/float(len(sixCount))

print ‘Percentage of times one or more six is rolled with six dice = ‘, 100*average

**#———for twelve dice————————**

j = 0

sixCount = []

while j < BIG_NUM:

i = 0

choices = []

while i <12:

sideChoice = die.makeRoll()

choices.append(sideChoice)

i = i + 1

sixCount.append(choices.count(6))

j = j + 1

average = 1- (sixCount.count(0) + sixCount.count(1)) /float(len(sixCount))

print ‘Percentage of times two or more sixes are rolled with twelve dice = ‘, 100*average

**#———for eighteen dice————————**

j = 0

sixCount = []

while j < BIG_NUM:

i = 0

choices = []

while i <18:

sideChoice = die.makeRoll()

choices.append(sideChoice)

i = i + 1

sixCount.append(choices.count(6))

j = j + 1

average = 1- (sixCount.count(0) + sixCount.count(1) + sixCount.count(2)) /float(len(sixCount))

print ‘Percentage of times three or more sixes are rolled with eighteen dice = ‘, 100*average

- Set of Six

from random import randint

**#define a class that creates the die**

class Die:

def __init__(self, sides):

self.sides = [i for i in range(1, sides + 1)]

**#function that makes each roll**

def makeRoll(self):

sideChoice = randint(1,6)

for item in self.sides:

if item == sideChoice:

self.sides.remove(sideChoice)

return self.sides

**#function that will renew the list**

def renewList(self, sides):

self.sides = [i for i in range(1, sides + 1)]

return self.sides

**#function that actually rolls the die as long as there are still**

**#numbers that have not yet been rolled**

def rollThatDie():

count = 0

while len(die.sides) > 0:

die.makeRoll()

count = count + 1

return count

**#function that determines the average number of rolls taken**

def average(count, BIG_NUM):

return count/float(BIG_NUM)

print ‘Enter the number of trials to run: ‘

BIG_NUM = raw_input()

BIG_NUM = int(BIG_NUM)

totalRolls = 0

i = 0

while i < BIG_NUM:

die = Die(6)

rollsToComplete = rollThatDie()

totalRolls = totalRolls + rollsToComplete

i = i + 1

print ‘Average number of rolls is: ‘, average(totalRolls, BIG_NUM)