Created
August 4, 2015 20:12
-
-
Save jayunit100/f0ca22f28e4057431e42 to your computer and use it in GitHub Desktop.
bps roulette wheel !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'jayvyas' | |
import random | |
import math | |
def roulette_wheel(model): | |
wheel = [None] * len(model) | |
cumProb = 0.0 | |
for i in range(len(model)): | |
(item, prob) = model[i] | |
print(i,item,prob) | |
cumProb += prob | |
wheel[i] = (item, cumProb) | |
return wheel | |
## Corresponds to the following roulette wheel... | |
## [.1,.8, .9, .95, 1.0] | |
wheel = roulette_wheel( | |
[ | |
("fish food",0.1), | |
("dog food",0.7), | |
("bird food",0.1), | |
("small rodent food",0.05), | |
("reptile food",0.05) | |
]) | |
for i in range(1000): | |
r = random.random() | |
print([it for (it, cp) in wheel if cp >= r ][0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dog food
fish food
dog food
dog food
dog food
fish food
dog food
dog food
bird food
dog food
fish food
dog food
dog food
dog food
dog food
small rodent food
dog food
dog food