Python与轮盘赌选择

import numpy as np
import logging
logging.basicConfig(level=logging.WARNING, format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s:%(message)s',
                    datefmt='%H:%M:%S')

def chooseBaseValue(val=[1, 2, 3, 4]):
    # 排序
    idx = np.argsort(val)  # 保留原索引
    val_ = np.sort(val)  # 值排序
    prob = [_/sum(val) for _ in val_]  # 概率
    dprob = np.cumsum(prob)  # 累积概率
    seed = np.random.random()  # 随机数

    res = idx[(dprob>seed).tolist().index(True)]  # 选择

    logging.warning(f" {res}:{seed:.3f}, {val} {dprob}")
    return res

 

posted @ 2022-10-31 17:01  ddzhen  阅读(233)  评论(0编辑  收藏  举报