[FRPythoneers] Weighted List
jack at performancedrivers.com
Tue May 6 14:49:12 MDT 2003
On Tue, May 06, 2003 at 11:42:08AM -0600, Dhruva B. Reddy wrote:
> I would like to choose elements from a list at random, but weight these
> elements. Has anyone done this?
Untested, but close to correct.
seed = random.Random()
"""I think the random module implements this"""
return int(size * seed.random())
# get twice as many Cs as Bs, and twice as many Bs as As
items_and_weights = [('A', 1), ('B', 2), ('C', 4)]
lookup_list = 
for (item, weight) in items_and_weights:
lookup_list.extend([item] * weight)
# pick a random item from the weighted list
ind = myrand(len(lookup_list))
More information about the FRPythoneers