how to choose element from list based on probabilities?
Jegenye 2001 Bt
jegenye2001 at fw.hu
Thu Nov 13 17:29:55 EST 2003
Is this a school homework? ;)
0/ add a method to your class Circle which computes the area
1/ make the list (list comprehension!) with tuples of the form
(circleobject, circleobject.area() )
2/ normalize the values to one, i.e. change the the list elements into a
form like (circleobject, circleobject.area()/S) ,
where S is the sum of the areas of all the circles
3/ sort the elements according to the normalized value so that they are in
increasing order
3/ roll a uniform number between 0 and 1 with using the random module
4/ find which element is the first one in the sequence where the normalized
value is already greater than (or equal to) this random value
Use a seed taken from, say, the current time for the random number generator
so that the subsequent calls are randomized.
Best,
Miklós
> I want to write a function that accepts a list of circle objects and
> then chooses one of the circles and returns that circle. The circles
> with the biggest areas should be the most likely to be chosen, but there
> should be some randomness.
>
> Subsequent calls to the function should not return the same circle.
>
> Can anyone help me out?
More information about the Python-list
mailing list