随机浮点数的数学方程?

发布于 2024-12-17 16:43:56 字数 1646 浏览 2 评论 0原文

我有这个方法:

- (float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2 {
return ((float)arc4random() / ARC4RANDOM_MAX) * num2-num1 + num1;
}

而且我很好奇是否有可能而不是使用以下条件: 我想为我的游戏制作一个随机浮点,如下所示:

When the score is:
Score 0-20: I want a float between 4.0-4.5 using the above method
Score 21-40: I want a float between 3.0-3.5 using the above method
Score 41-60: I want a float between 2.5-3.0 using the above method
Score 61+: I want a float between 2.0-2.5 using the above method

现在我知道我可以使用条件来做到这一点,但是有没有比这样做更容易的数学方程?

谢谢!

编辑1:

    - (float)determineFloat {
    if (score <= 60)
    {
        //Gets the tens place digit, asserting >= 0.
        int f = fmax(floor( (score - 1) / 10 ), 0);

        switch (f)
        {
            case 0:
            case 1:
            {
                // return float between 4.0 and 4.5
                [self randomFloatBetween:4.0 andLargerFloat:4.5];
            }
            case 2:
            case 3:
            {
                // return float between 3.0 and 3.5
                [self randomFloatBetween:3 andLargerFloat:3.5];
            }
            case 4:
            case 5:
            {
                // return float between 2.5 and 3.0
                [self randomFloatBetween:2.5 andLargerFloat:3];
            }
            default:
            {
                return 0;
            }
        }
    }
    else
    {
        // return float between 2.0 and 2.5
        [self randomFloatBetween:2.0 andLargerFloat:2.5];
    }
    return;
}

这怎么样?另外,您确定这是最有效的方法吗?

I have this method:

- (float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2 {
return ((float)arc4random() / ARC4RANDOM_MAX) * num2-num1 + num1;
}

And I am curious that if it is possible instead of using conditionals for the following:
I want to make a random float for my game like this:

When the score is:
Score 0-20: I want a float between 4.0-4.5 using the above method
Score 21-40: I want a float between 3.0-3.5 using the above method
Score 41-60: I want a float between 2.5-3.0 using the above method
Score 61+: I want a float between 2.0-2.5 using the above method

Now I know I can use conditionals to do that but is there any math equation that would be easier than doing that?

Thanks!

Edit1:

    - (float)determineFloat {
    if (score <= 60)
    {
        //Gets the tens place digit, asserting >= 0.
        int f = fmax(floor( (score - 1) / 10 ), 0);

        switch (f)
        {
            case 0:
            case 1:
            {
                // return float between 4.0 and 4.5
                [self randomFloatBetween:4.0 andLargerFloat:4.5];
            }
            case 2:
            case 3:
            {
                // return float between 3.0 and 3.5
                [self randomFloatBetween:3 andLargerFloat:3.5];
            }
            case 4:
            case 5:
            {
                // return float between 2.5 and 3.0
                [self randomFloatBetween:2.5 andLargerFloat:3];
            }
            default:
            {
                return 0;
            }
        }
    }
    else
    {
        // return float between 2.0 and 2.5
        [self randomFloatBetween:2.0 andLargerFloat:2.5];
    }
    return;
}

Hows this?. Also are you sure this is the most efficient way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2024-12-24 16:43:56

可能不是,因为这种关系不是连续的。当你有这种需求时,最好只使用条件语句或 switch 语句。然后,您和任何阅读或调试代码的人都会确切地知道该函数正在做什么。在这种情况下使用某种数学函数,这将是极其复杂的,充其量,很可能会减慢进程。

使用开关的可能性:

-(float)determineFloat:(float)score
{
    if (score <= 60)
    {
        //Gets the tens place digit, asserting >= 0.
        int f = (int)fmax(floor( (score - 1) / 10.0f ), 0);

        switch (f)
        {
            case 0:
            case 1:
            {
                return [self randomFloatBetween:4.0 andLargerFloat:4.5];
            }
            case 2:
            case 3:
            {
                return [self randomFloatBetween:3.0 andLargerFloat:3.5];
            }
            case 4:
            case 5:
            {
                return [self randomFloatBetween:2.5 andLargerFloat:3.0];
            }
            default:
            {
                return 0;
            }
        }
    }
    else
    {
        return [self randomFloatBetween:2.0 andLargerFloat:2.5];
    }
}

用法:

float myScore = 33;
float randomFloat = [self determineFloat:myScore];

现在,randomFloat 将是 3 到 3.5 之间的值。

Probably not, since the relation is not continuous. When you have this kind of requirement, it is better to just use conditionals or a switch statement. You and anyone reading or debugging the code would then know exactly what the function is doing. Using some sort of mathematical function in this case, which would be extremely complicated, at best, is most likely going to slow the process down.

Possibility using a switch:

-(float)determineFloat:(float)score
{
    if (score <= 60)
    {
        //Gets the tens place digit, asserting >= 0.
        int f = (int)fmax(floor( (score - 1) / 10.0f ), 0);

        switch (f)
        {
            case 0:
            case 1:
            {
                return [self randomFloatBetween:4.0 andLargerFloat:4.5];
            }
            case 2:
            case 3:
            {
                return [self randomFloatBetween:3.0 andLargerFloat:3.5];
            }
            case 4:
            case 5:
            {
                return [self randomFloatBetween:2.5 andLargerFloat:3.0];
            }
            default:
            {
                return 0;
            }
        }
    }
    else
    {
        return [self randomFloatBetween:2.0 andLargerFloat:2.5];
    }
}

Usage:

float myScore = 33;
float randomFloat = [self determineFloat:myScore];

Now, randomFloat will be a value between 3 and 3.5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文