primitive.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:epi 作者: jakubtuchol 项目源码 文件源码
def uniform_random(lower_bound, upper_bound):
    """
    Question 5.10: Generates uniform random number
    within lower and upper bounds, inclusive
    """
    num_outcomes = upper_bound - lower_bound + 1
    result = None

    while True:
        result = 0
        i = 0
        while 1 << i < num_outcomes:
            result = (result << 1) | getrandbits(1)
            i += 1
        if result < num_outcomes:
            break
    return result + lower_bound
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号