정해진 확률로 대상 선택하기

import random

def get_index(probability):
    ex_probability=0
    sum_probability=[]
    for p in probability:
        ex_probability+=p
        sum_probability.append(ex_probability)
    r=random.random()
    for index, sp in enumerate(sum_probability):
        if r<=sp:
            return index
    return len(probability)-1

dollar=['a','b','c']
pro=[0.5,0.3,0.2]
pro_index=get_index(pro)
print(dollar[pro_index])

0.5의 확률로 a 선택, 0.3의 확률로 b 선택, 0.2의 확률로 c 선택하기