Probability Algorithm

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Aug 26 05:47:56 CEST 2012


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


More information about the Python-list mailing list