gpt4 book ai didi

javascript - 使用 Math.random() 获得 50/50 机会的正确函数

转载 作者:数据小太阳 更新时间:2023-10-29 05:41:36 24 4
gpt4 key购买 nike

哪个是获得精确 50/50 机会的正确函数:

return Math.random() < 0.5;

对比

return Math.random() <= 0.5;

最佳答案

Math.random() :

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1); that is, from 0 (inclusive) up to but not including 1 (exclusive)

随机数在 [0,0.5) 范围内或 [0.5,1) .所以你应该使用 return Math.random() < 0.5;有(理论上的)50/50 的机会。

关于javascript - 使用 Math.random() 获得 50/50 机会的正确函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44651537/

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