gpt4 book ai didi

python - randrange() 函数如何工作?

转载 作者:行者123 更新时间:2023-11-28 20:47:04 24 4
gpt4 key购买 nike

我一直想知道 python 中的 random.randrange() 函数究竟是如何返回的在不是二的幂的范围内具有均匀偏差的值(这很容易,因为 N 位是随机产生的)

提前致谢!

最佳答案

快速查看源代码后,这是基本原理:

def randrange(start, stop, step)
width = stop - start
n = (width + step - 1) // step
return start + step * int(random() * n)

关于python - randrange() 函数如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19633241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com