import os, sys import random random.seed(12345) stone_combinations = [('W', 'Y'), ('W', 'B1'), ('W', 'B2'), ('Y', 'B1'), ('Y', 'B2'), ('B1', 'B2')] stone_permutations = [('W', 'Y'), ('W', 'B1'), ('W', 'B2'), ('Y', 'B1'), ('Y', 'B2'), ('B1', 'B2'), ('Y', 'W'), ('B1', 'W'), ('B2', 'W'), ('B1', 'Y'), ('B2', 'Y'), ('B2', 'B1') ] def stoneColor(stone): """Pick stone color from entry -- needed because we represent two different blues as 'B1' and 'B2'""" return stone[0:1] def simIfAtLeastOneBlueCombo(trials): """Simulation: Pick combination of two, check both stones, proceed only if at least one of the stones is blue.""" numTrialsAdmitted = 0 numTrialsPassed = 0 for i in xrange(trials): choice = random.choice(stone_combinations) if stoneColor(choice[0]) == 'B' or stoneColor(choice[1]) == 'B': numTrialsAdmitted += 1 if choice == ('B1', 'B2'): numTrialsPassed += 1 print "Simulation: Pick combination of two, check both stones, proceed only if at least one of the stones is blue." print "-"*80 print "Total Sims: %d, Trials Admitted = %d, Trials Passed = %d, Percent of Passing over Admitted: %6.2f%%" % (trials, numTrialsAdmitted, numTrialsPassed, numTrialsPassed / float(numTrialsAdmitted) * 100 ) print def simIfRandomStoneIsBlueCombo(trials): """Simulation: Pick combination of two, randomly pick a stone, proceed only if that stone is blue.""" numTrialsAdmitted = 0 numTrialsPassed = 0 for i in xrange(trials): choice = random.choice(stone_combinations) random_stone_from_choice = random.choice(choice) if stoneColor(random_stone_from_choice) == 'B': numTrialsAdmitted += 1 if choice == ('B1', 'B2'): numTrialsPassed += 1 print "Simulation: Pick combination of two, randomly pick a stone, proceed only if that stone is blue." print "-"*80 print "Total Sims: %d, Trials Admitted = %d, Trials Passed = %d, Percent of Passing over Admitted: %6.2f%%" % (trials, numTrialsAdmitted, numTrialsPassed, numTrialsPassed / float(numTrialsAdmitted) * 100 ) print def simIfFirstBluePerm(trials): """Simulation: Pick permutation of two, check first stone, proceed only if first stone is blue.""" numTrialsAdmitted = 0 numTrialsPassed = 0 for i in xrange(trials): choice = random.choice(stone_permutations) if stoneColor(choice[0]) == 'B': numTrialsAdmitted += 1 if choice == ('B1', 'B2') or choice == ('B2', 'B1'): numTrialsPassed += 1 print "Simulation: Pick permutation of two, check first stone, proceed only if first stone is blue." print "-"*80 print "Total Sims: %d, Trials Admitted = %d, Trials Passed = %d, Percent of Passing over Admitted: %6.2f%%" % (trials, numTrialsAdmitted, numTrialsPassed, numTrialsPassed / float(numTrialsAdmitted) * 100 ) print # The simulations simIfAtLeastOneBlueCombo(10000) simIfRandomStoneIsBlueCombo(10000) simIfFirstBluePerm(10000) # ================================================================================ # Output # ================================================================================ # Simulation: Pick combination of two, check both stones, proceed only if at least one of the stones is blue. # -------------------------------------------------------------------------------- # Total Sims: 10000, Trials Admitted = 8306, Trials Passed = 1651, Percent of Passing over Admitted: 19.88% # # Simulation: Pick combination of two, randomly pick a stone, proceed only if that stone is blue. # -------------------------------------------------------------------------------- # Total Sims: 10000, Trials Admitted = 4967, Trials Passed = 1674, Percent of Passing over Admitted: 33.70% # # Simulation: Pick permutation of two, check first stone, proceed only if first stone is blue. # -------------------------------------------------------------------------------- # Total Sims: 10000, Trials Admitted = 4892, Trials Passed = 1715, Percent of Passing over Admitted: 35.06%