Skip to content

Instantly share code, notes, and snippets.

@ShaneK
Last active December 15, 2015 21:48
Show Gist options
  • Save ShaneK/5327897 to your computer and use it in GitHub Desktop.
Save ShaneK/5327897 to your computer and use it in GitHub Desktop.
Python method I plan to use to help me with markov chains.
import random
class PercentagePicker:
def PickRandomWord(self, setOfNumbers):
setOfRanges = []
maxInteger = -1
for miniSet in setOfNumbers:
temp = [miniSet[0], maxInteger + 1, maxInteger + miniSet[1]]
maxInteger += miniSet[1]
setOfRanges.append(temp)
value = random.randint(0, maxInteger)
for miniSet in setOfRanges:
if miniSet[1] <= value <= miniSet[2]:
return miniSet[0]
pp = PercentagePicker()
range1 = ["Shane", 1] #10%
range2 = ["Rocks", 3] #30%
range3 = ["Your Socks", 6] #60%
range1int = 0
range2int = 0
range3int = 0
finalSet = [range1, range2, range3]
for i in range(0, 10000):
word = pp.PickRandomWord(finalSet)
if word == "Shane":
range1int += 1
elif word == "Rocks":
range2int += 1
else:
range3int += 1
print "Shane: " + str(range1int) + "\n Rocks: " + str(range2int) + "\n Your Socks: " + str(range3int)
#Prints something like this, which is about right considering the percentages
#Shane: 976
#Rocks: 2993
#Your Socks: 6031
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment