Re: implement random selection in Python

2007-11-19 Thread Scott David Daniels
J. Clifford Dyer wrote: > My understanding of what you are looking for is that on each individual > selection, > the probability of picking a given name is that name's prob value, divided by > the > sum of all prob values. That is, in the following case: > items = [('Mary', 96),('Shimon', 1)

Re: implement random selection in Python

2007-11-19 Thread Neil Cerutti
On 2007-11-17, Bruza <[EMAIL PROTECTED]> wrote: > OOPS. I pressed the Send too fast. > > The problem w/ Boris's solution is that after repeated calling > of randomPick(3,items), 'Jane' is not the most "frequent > appearing" member in all the out list of 3 member lists... How does this solution fai

Re: implement random selection in Python

2007-11-17 Thread Steven D'Aprano
On Sat, 17 Nov 2007 13:51:16 -0800, [EMAIL PROTECTED] wrote: > I am not guaranteeing this even works. I am seeing that there is some > collision among the numbers, but it will work for the most part. "Work for the most part" -- is that another way of saying "Apart from the bugs, this is bug-free

Re: implement random selection in Python

2007-11-17 Thread [EMAIL PROTECTED]
Knuth says to pick N distinct records from a collection where the probability is equal you should: first fill up N records by chosing the first seen. if less than N were in the collection, quit. otherwise, t = (the number of items looked at) or N to start. while your not at the end of the colle

Re: implement random selection in Python

2007-11-17 Thread J. Clifford Dyer
On Fri, 2007-11-16 at 16:47 -0800, Bruza wrote: I think I need to explain on the probability part: the "prob" is a > relative likelihood that the object will be included in the output > list. So, in my example input of > > items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > So, f

Re: implement random selection in Python

2007-11-16 Thread Jordan
How about this variation on your intial attempt? # Untested! def randomPick(n, items): def pickOne(): index = random.randint(0, 99) currentP = 0 for (obj, p) in items: currentP += p if currentP > index: return obj selection = set() while len(selection) < n:

Re: implement random selection in Python

2007-11-16 Thread Steven D'Aprano
On Fri, 16 Nov 2007 16:47:16 -0800, Bruza wrote: > I think I need to explain on the probability part: the "prob" is a > relative likelihood that the object will be included in the output list. > So, in my example input of > > items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > S

Re: implement random selection in Python

2007-11-16 Thread duncan smith
Bruza wrote: > On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]> wrote: >> On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> >> wrote: >> >> >> >>> Bruza wrote: I need to implement a "random selection" algorithm which takes a list of [(obj, prob),...] as input. Each of the (obj, prob) repr

Re: implement random selection in Python

2007-11-16 Thread Jordan
Maybe it would help to make your problem statement a litte rigorous so we can get a clearer idea of whats required. One possible formulation: Given a list L of pairs of values, weightings: [ (v_0, w_0), (v_1, w_1), ], and some N between 1 and length(L) you would like to randomly select a set

Re: implement random selection in Python

2007-11-16 Thread Bruza
On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]> wrote: > On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> > wrote: > > > > > Bruza wrote: > > > I need to implement a "random selection" algorithm which takes a list > > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > > l

Re: implement random selection in Python

2007-11-16 Thread Bruza
On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> wrote: > Bruza wrote: > > I need to implement a "random selection" algorithm which takes a list > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > likely an object, "obj", should be selected based on its probability > >

Re: implement random selection in Python

2007-11-16 Thread duncan smith
Bruza wrote: > I need to implement a "random selection" algorithm which takes a list > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > likely an object, "obj", should be selected based on its probability > of > "prob".To simplify the problem, assuming "prob" are integers, an

Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Boris Borcic wrote: > Bruza wrote: >> No. That does not solve the problem. What I want is a function >> >> def randomPick(n, the_items): >> >> which will return n DISTINCT items from "the_items" such that >> the n items returned are according to their probabilities specified >> in the (item, pro)

Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Bruza wrote: > No. That does not solve the problem. What I want is a function > > def randomPick(n, the_items): > > which will return n DISTINCT items from "the_items" such that > the n items returned are according to their probabilities specified > in the (item, pro) elements inside "the_items

Re: implement random selection in Python

2007-11-16 Thread Paul Rubin
Bruza <[EMAIL PROTECTED]> writes: > But how about the general case, for N > 1 and N < len(items)? Is there > some clever algorithm using Python standard "random" package Yeah, I'm not sure what the name for it is, but there'ss a well known algorithm that's sort of an online verison of random.choic

Re: implement random selection in Python

2007-11-16 Thread bearophileHUGS
This recipe of mine may help: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498229 Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Bruza wrote: > No. That does not solve the problem. What I want is a function > > def randomPick(n, the_items): > > which will return n DISTINCT items from "the_items" such that > the n items returned are according to their probabilities specified > in the (item, pro) elements inside "the_items

Re: implement random selection in Python

2007-11-15 Thread Bruza
On Nov 15, 9:32 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Nov 15, 10:40�pm, Bruza <[EMAIL PROTECTED]> wrote: > > > > > I need to implement a "random selection" algorithm which takes a list > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > likely an object,

Re: implement random selection in Python

2007-11-15 Thread [EMAIL PROTECTED]
On Nov 15, 10:40�pm, Bruza <[EMAIL PROTECTED]> wrote: > I need to implement a "random selection" algorithm which takes a list > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > likely an object, "obj", should be selected based on its probability > of > "prob".To simplify the

Re: implement random selection in Python

2007-11-15 Thread James Stroud
Bruza wrote: > I need to implement a "random selection" algorithm which takes a list > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > likely an object, "obj", should be selected based on its probability > of > "prob".To simplify the problem, assuming "prob" are integers, an

implement random selection in Python

2007-11-15 Thread Bruza
I need to implement a "random selection" algorithm which takes a list of [(obj, prob),...] as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of "prob".To simplify the problem, assuming "prob" are integers, and the sum of all "prob"