0

在我的游戏中,我知道我可以为此使用条件,但我想看看是否有一个数学方程式可以让我更轻松。我想用arc4random().

我需要的是:

  1. 如果我的分数是 1-20,概率:25 分中的 1 分
  2. 如果我的分数是 21-40,概率:20 分中的 1 分
  3. 如果我的分数是 41-60,概率:1 分 15 分
  4. 如果我的分数是 60+,则概率:10 分中的 1 分。

这可能吗?如果是这样,我将如何实现这一目标?

谢谢!

4

1 回答 1

1

这应该可以解决问题(希望很清楚!):

int modNumber = 25;
float alteredScore = score - (floor((score-1)/60) - 1) * 60;
modNumber -= floor((alteredScore-1)/20) * 5;
int result = arc4random() % modNumber;  // Or arc4random_uniform(modNumber) if you want to completely remove modulo bias, but beaing in mind the non-randomness (!) of arc4random and the size of modNumber I highly doubt it would make any real difference
if (!result) {
    // Result is 0
    // Do stuff for the lucky person
} else {
    // Make them miserable
}
于 2011-11-18T22:43:57.840 回答