how to choose element from list based on probabilities?

John Roth newsgroups at jhrothjr.com
Thu Nov 13 17:06:22 EST 2003


"Matthew Wilson" <mwilson at sarcastic-horse.com> wrote in message
news:slrnbr81e4.gp0.mwilson at overlook.homelinux.net...
> I have a list of very simple circle objects:
>
> class Circle:
>     def __init__(self, x,y,r):
>         self.center =(x,y)
>         self.r = r
>
> 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?

Look at module "random". I'd suggest sorting in size order
and then applying some variety of bias function, and then removing
the selected item from the list.

John Roth






More information about the Python-list mailing list