Skip to content

Instantly share code, notes, and snippets.

@jayunit100
Created August 4, 2015 20:12
Show Gist options
  • Save jayunit100/f0ca22f28e4057431e42 to your computer and use it in GitHub Desktop.
Save jayunit100/f0ca22f28e4057431e42 to your computer and use it in GitHub Desktop.
bps roulette wheel !
__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])
@jayunit100
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment