Probability Algorithm

月忧茗 yueyoum at gmail.com
Sat Aug 25 12:03:07 EDT 2012


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%




I have written some code, but this just for  the four lists are have a lots
of elements.

----

from random import choice

final_list = []
slot = []

a_picked_times = 0

while a_picked_times < 43:
    item = choice(ALIST)
    ALIST.remove(item)

    if item in already_picked_list:
        continue

    slot.append(item)
    a_picked_times += 1


b_picked_times = 0

while_b_picked_times < 37:
    ...

SOME CODE SIMILAR

# now slot is a list which contains 100 elements,
# in slot, there are 43 elements of ALIST'items, 37 of B, 19 of C, 1 of D

for i in range(3):
    final_list.append( choice(slot) )

----

So, this can ensure the probability requirements.  *BUT only under the
condition: this Four lists have a lots of elements.
*
list.remove( item )  that will not remove all elements in list,  so  we
will correct pick up items with the needs times.

But,  when A, B, C, D empty OR  not enough elements,  How could ensure the
probability requirements?



A, B, C, D list are all get from redis sorted list.   Or some solution with
redis ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120826/089bb4ee/attachment.html>


More information about the Python-list mailing list