Re: Probability Algorithm

Discussion in 'Python' started by Dave Angel, Aug 25, 2012.

  1. Dave Angel

    Dave Angel Guest

    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
    Dave Angel, Aug 25, 2012
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Aug 26, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary N.
    Replies:
    4
    Views:
    1,623
    Gary N.
    Feb 22, 2004
  2. Digital Puer
    Replies:
    5
    Views:
    11,825
    marcus
    Nov 29, 2004
  3. Replies:
    1
    Views:
    331
    el goog
    Mar 20, 2005
  4. Dennis Lee Bieber

    Re: Probability Algorithm

    Dennis Lee Bieber, Aug 25, 2012, in forum: Python
    Replies:
    0
    Views:
    147
    Dennis Lee Bieber
    Aug 25, 2012
  5. Dennis Lee Bieber

    Re: Probability Algorithm

    Dennis Lee Bieber, Aug 26, 2012, in forum: Python
    Replies:
    0
    Views:
    154
    Dennis Lee Bieber
    Aug 26, 2012
Loading...

Share This Page