Prev: Pick items from list with probability based upon property oflist member ?
Next: setup server from scratch (with or without apache?)
From: Rob Williscroft on 20 Jun 2010 06:55
southof40 wrote in news:da3cc892-b6dd-4b37-a6e6-
b606ef9670fa(a)t26g2000prt.googlegroups.com in gmane.comp.python.general:
> I have list of of N Vehicle objects - the only possible vehicles are
> cars, bikes, trucks.
> I want to select an object from the list with a probability of : cars
> 0.7, bikes 0.3, trucks 0.1.
Aside, all your probabilities add up to 1.1, they should add up to 1.
> I've currently implemented this by creating another list in which each
> car object from the original list appears 7 times, each bike 3 times
> and each truck once. I then pick at random from that list.
Aside, so 7 / 11 bikes, 3 / 11 cars and 1 / 11 trucks, are your
But to answer your question, you could create 3 list, and then
pick the list you draw from based on a random number then pick
the item from the list based on another random number:
r = ( random() * 11 )
if r < 1:
picklist = truck_list
elif r < 4:
picklist = bike_list
picklist = car_list
# now pick the final item from pick list.