Velocity Reviews > Re: Probability Algorithm

# Re: Probability Algorithm

Dave Angel
Guest
Posts: n/a

 08-25-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. *
> ALIST, BLIST, CLIST, DLIST
>
> 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
100%.

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.

--

DaveA

Steven D'Aprano
Guest
Posts: n/a

 08-26-2012
On 08/25/2012 12:03 PM, 月忧茗 wrote:

> 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%

First, select one of the four lists with those appropriate probabilities.
Then once you selected a list, select one of its items randomly.

import random

def select_list():
x = random.randint(1, 100)
if x <= 43:
return ALIST
elif x <= 80: # 43 + 37
return BLIST
elif x <= 99: # + 19
return CLIST
else:
return DLIST

the_list = select_list()
the_item = random.choice(the_list)

--
Steven

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Dennis Lee Bieber Python 0 08-26-2012 09:43 PM Dennis Lee Bieber Python 0 08-25-2012 08:47 PM siddharth_jain_1@rediffmail.com Java 1 03-20-2005 06:43 PM Digital Puer Java 5 11-29-2004 05:57 AM Gary N. Java 4 02-22-2004 05:43 PM