Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27879 > unrolled thread

Re: Probability Algorithm

Started byDave Angel <d@davea.name>
First post2012-08-25 12:37 -0400
Last post2012-08-26 03:47 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Probability Algorithm Dave Angel <d@davea.name> - 2012-08-25 12:37 -0400
    Re: Probability Algorithm Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-26 03:47 +0000

#27879 — Re: Probability Algorithm

FromDave Angel <d@davea.name>
Date2012-08-25 12:37 -0400
SubjectRe: Probability Algorithm
Message-ID<mailman.3808.1345912705.4697.python-list@python.org>
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

[toc] | [next] | [standalone]


#27900

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-08-26 03:47 +0000
Message-ID<50399c6c$0$6574$c3e8da3$5496439d@news.astraweb.com>
In reply to#27879
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web