Java概率(基于百分比)3个选项



我只是想确认这个代码对我正在寻找的东西是正确的。基本上,我希望在调用该函数时从某个稀有值中取出一个数字。现在的罕见概率是15%,20%和65%的常见概率。然而,重读我的代码后,似乎是20%的机会实际上是低于15%的机会,这将是不理想的。有人能帮我检查一下吗?谢谢!

public void roll() {
float rarity = random.nextFloat();
if(rarity <= 0.15f) {
// Roll Legendary Item
} else if(rarity <= 0.35) {
// Roll Rare Item
} else {
// Roll Common Item
}
}

应该是

else if(rarity <= 0.35f) 

你写代码的方式,它目前是15%-5%-80%

相关内容

最新更新