Hi, All, <br><br>I have a problem of probability algorithm<br><br><br>The goal is obtain a list which contains three items. as the <b>FinalList</b><br><br>There has Four source lists. <b><br>ALIST, BLIST, CLIST, DLIST<br>
<br>There are all Unknown length. They contains unique elements</b><br>( In fact, there are all empty at the program beginning, when running, there growing )<br><br>Choose items form this source lists. pick up random items to generate the FinalList<br>
Ensure The Following Requirements<br><br>In the FinalList, <br>probability of ALIST's item appeared is 43%<br>probability of BLIST's item appeared is 37%<br>probability of CLIST's item appeared is 19%<br>
probability of DLIST's item appeared is 1%<br><br><br><br><br>I have written some code, but this just for the four lists are have a lots of elements.<br><br>----<br><br>from random import choice<br><br>final_list = []<br>
slot = []<br><br>a_picked_times = 0<br><br>while a_picked_times < 43:<br> item = choice(ALIST)<br> ALIST.remove(item)<br><br> if item in already_picked_list:<br> continue<br> <br> slot.append(item)<br>
a_picked_times += 1<br><br><br>b_picked_times = 0<br><br>while_b_picked_times < 37:<br> ...<br><br>SOME CODE SIMILAR<br><br># now slot is a list which contains 100 elements, <br># in slot, there are 43 elements of ALIST'items, 37 of B, 19 of C, 1 of D<br>
<br>for i in range(3):<br> final_list.append( choice(slot) )<br><br>----<br><br>So, this can ensure the probability requirements. <b>BUT only under the condition: this Four lists have a lots of elements.<br></b><br>list.remove( item ) that will not remove all elements in list, so we will correct pick up items with the needs times.<br>
<br>But, when A, B, C, D empty OR not enough elements, How could ensure the probability requirements?<br><br><br><br>A, B, C, D list are all get from redis sorted list. Or some solution with redis ?<br>