how to choose element from list based on probabilities?

Jegenye 2001 Bt jegenye2001 at fw.hu
Fri Nov 14 15:01:37 EST 2003


Well, giving it a second thought,  it's perhaps better to build up a list of
the cumulative sums of the normalized areas
,instead of sorting according to the normalized area, and then do the
comparison against that.
[narea1, narea1+narea2, narea1+narea2+narea3, ....]

Miklós


Jegenye 2001 Bt <jegenye2001 at fw.hu> wrote in message
news:bp10k7$k32$1 at namru.matavnet.hu...
> 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
> 4/ roll a uniform number between 0 and 1 with using the random module
> 5/ 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
>
>
>







More information about the Python-list mailing list