Re: Probability Algorithm

Discussion in 'Python' started by Dennis Lee Bieber, Aug 25, 2012.

  1. On Sun, 26 Aug 2012 00:03:07 +0800, ??? <> declaimed
    the following in gmane.comp.python.general:

    > Hi, All,
    >
    > I have a problem of probability algorithm
    >

    Smells like homework -- but since you did provide something that
    could be turned in as your work, I'm going to go out on a limb

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


    theLists = { "A" : [],
    "B" : [],
    "C" : [],
    "D" : [] }

    # do whatever is needed to populate the lists
    # I presume reading some file(s) and doing
    ...
    theLists[listName].append(listValue)


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

    <snip>

    theProbabilities = { "A" : 43,
    "B" : 37+43,
    "C" : 19+37+43,
    "D" : 1+19+37+43 }


    > while a_picked_times < 43:


    Why keep a counter? Rather than an iterated loop

    for i in range(43):

    > item = choice(ALIST)
    > ALIST.remove(item)


    Do you really want to remove an item from the source list?
    Technically, a requirement that an item appears from ALIST 43% of the
    time does NOT prohibit it being the SAME ITEM. {This also answers you
    problem about only working if the lists are long... as long as the list
    contains at least ONE item, you can pick that item to meet the
    probability}

    >
    > if item in already_picked_list:
    > continue
    >

    This is meaningless with regards to the previous comments: if you
    had removed the item from the source list, it will never appear again
    (if it did, it was from a different position in the source list, or from
    a different source list overall -- and if you don't want duplicates from
    within a source list, you should remove them when building the source
    list)

    > slot.append(item)
    > a_picked_times += 1
    >


    <snip>

    Given that "theLists" contains the four lists:

    result = []
    while len(result) < 3:
    #using this test handles the case of sublist being empty
    x = random.randint(0, 99)
    if x < theProbabilities["A"]:
    clist = theLists["A"]
    elif x < theProbabilities["B"]:
    clist = theLists["B"]
    elif x < theProbabilities["C"]:
    clist = theLists["C"]
    else:
    clist = theLists["D"]

    if len(clist):
    #is sublist is empty, nothing gets appended, loop goes on
    result.append(random.choice(clist))

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Aug 25, 2012
    #1
    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,658
    Gary N.
    Feb 22, 2004
  2. Digital Puer
    Replies:
    5
    Views:
    11,978
    marcus
    Nov 29, 2004
  3. Replies:
    1
    Views:
    339
    el goog
    Mar 20, 2005
  4. Dave Angel

    Re: Probability Algorithm

    Dave Angel, Aug 25, 2012, in forum: Python
    Replies:
    1
    Views:
    185
    Steven D'Aprano
    Aug 26, 2012
  5. Dennis Lee Bieber

    Re: Probability Algorithm

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

Share This Page