Search code examples
iosmathfloating-pointarc4random

Math equation for random float?


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?


Solution

  • 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.