Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:36:53+00:00 2026-05-27T00:36:53+00:00

I have this method: – (float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2 { return ((float)arc4random() / ARC4RANDOM_MAX) * num2-num1

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:36:54+00:00Added an answer on May 27, 2026 at 12:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method that is supposed to take a screenshot and return the
I have this method that changes an larger image source on click. I need
I have this method def last_board user = current_user #current_user boards = current_user.boards #return
I have this method that downloads .csv files from yahoo finance and saves them
I have this method in my book model: def get_absolute_url(self): return /book/%s/%s/%i/%i/ % (
I have this method public Expression<Func<Auction, bool>> GetUnsetDatesAuctionsExpression() { if (condition) return GetAllUnsetDatesAuctionsExpression(); else
I have this method in a Directory model: public List<Directory> subdirectories() { return find(select
I have this method, that will be called against from a WCF Client, but
I have this method for shrinking down an image for a website that I'm
I have this method, receive a list of integer, and must return an Collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.