Probability Algorithm

Dave Angel d at
Sat Aug 25 18:37:50 CEST 2012

On 08/25/2012 12:03 PM, 月忧茗 wrote:
> Hi,  All,
> I have a  problem of probability algorithm
> The goal is obtain a list which contains three items.   as the *FinalList*
> There has Four source lists. *
> There are all  Unknown length. They contains unique elements*
> ( In fact,  there are all empty at the program beginning,  when running,
> there growing  )
> Choose items form this source lists. pick up random items to generate the
> FinalList
> Ensure  The Following Requirements
> In the FinalList,
> probability of ALIST's item appeared  is  43%
> probability of BLIST's item appeared  is  37%
> probability of CLIST's item appeared  is  19%
> probability of DLIST's item appeared  is  1%

Would you like to tell us the actual assignment?  This looks like it's
paraphrased.  if you have 3 items, each coming from one of four lists,
the four probabilities have to add up to much more than 100%.

Perhaps what you meant was that each of the three items had those
probabilities of coming from the respective lists.  Then it'd add up to

Your code is far more complex than needed, and as you observed, doesn't
work if each list doesn't have sufficient members.

I'd simply pick a random number from 0 to 99, see if it's less than 43
and if so, use ALIST.  Else if it's less than 80, use BLIST.  else if
it's less than 99, use CLIST.  Else DLIST.

Then do that 2 more times and you're done.

Don't forget to factor the problem into functions, so you can easily
repeat similar code.

If a list is picked, and it's empty, throw an exception.  Or wait till
the missing item arrives.  And you have to decide  whether to remove the
selected items from the respective lists.  That wasn't specified in the
problem statement.



More information about the Python-list mailing list