From: southof40 on
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.

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.

This works but seems very clunky to me. Can anyone suggest a better
data structure which would support the 'weighted randomness' I'm
after ?

I'm not fixed on the idea of using a list - could be a dictionary,
tree whatever .

Thanks in advance.

R.

From: southof40 on
Thanks to everybody ... as usual on c.l.p I'm blown away by the
knowledge and skills ! I've added some replies/clarifications to other
posts but thanks again to you all.
From: southof40 on
Oh yes as several have pointed out there was a typo in my original
question ... I can only blame 'toolongatscreenitis' !
From: southof40 on
On Jun 20, 10:53 pm, Steven D'Aprano <st...(a)REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 20 Jun 2010 03:19:55 -0700, southof40 wrote:
> > 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.
>
> That adds to a probability of 1.1, which is impossible.
>
> I'm going to assume that bikes is a typo and should be 0.2.
>
> cars = [car1, car2]
> bikes = [bike1, bike2, bike3, bike4, bike5, bike6]
> trucks = [truck1, truck2, truck3, truck4]
>
> x = random.random()
> if x < 0.7:
>     return random.choice(cars)
> elif x < 0.9:
>     return random.choice(bikes)
> else:
>     return random.choice(trucks)
>
> But surely this is not physically realistic? The probability of selecting
> a car would normally depend on the number of cars, and not be set before
> hand.
>
> --
> Steven

Nice solution thanks.
From: southof40 on
On Jun 20, 10:55 pm, Rob Williscroft <r...(a)rtw.me.uk> wrote:
> southof40 wrote in news:da3cc892-b6dd-4b37-a6e6-
> b606ef967...(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
> actual probabilities.
>
> 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
> else:
>   picklist = car_list
>
> # now pick the final item from pick list.
>
> Rob.

thanks also - nice clean solution