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

  • Home
  • SEARCH
  • 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 6786689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:16:33+00:00 2026-05-26T17:16:33+00:00

I have a float and I am trying to get a random number between

  • 0

I have a float and I am trying to get a random number between 1.5 – 2. I have seen tutorials on the web but all of them are doing the randomization for 0 to a number instead of 1.5 in my case. I know it is possible but I have been scratching my head on how to actually accomplish this. Can anyone help me?

Edit1: I found the following method on the web but I do not want all these decimals places. I only want things like 5.2 or 7.4 etc…

How would I adjust this method to do that?

-(float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2
{
    int startVal = num1*10000;
    int endVal = num2*10000; 

    int randomValue = startVal + (arc4random() % (endVal - startVal));
    float a = randomValue;

    return (a / 10000.0);
}

Edit2: Ok so now my method is like this:

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

Will this produce numbers like 1.624566 etc..? Because I only want say 1.5,1.6,1.7,1.8,1.9, and 2.0.

  • 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-26T17:16:34+00:00Added an answer on May 26, 2026 at 5:16 pm

    You can just produce a random float from 0 to 0.5 and add 1.5.

    EDIT:

    You’re on the right track. I would use the maximum random value possible as your divisor in order to get the smallest intervals you can between possible values, rather than this arbitrary division by 10,000 thing you have going on. So, define the maximum value of arc4random() as a macro (I just found this online):

    #define ARC4RANDOM_MAX      0x100000000
    

    Then to get a value between 1.5 and 2.0:

    float range = num2 - num1;
    float val = ((float)arc4random() / ARC4RANDOM_MAX) * range + num1;
    return val;
    

    This will also give you double precision if you want it (just replace float with double.)

    EDIT AGAIN:

    Yes, of course this will give you values with more than one decimal place. If you want only one, just produce a random integer from 15 to 20 and divide by 10. Or you could just hack off the extra places afterward:

    float range = num2 - num1;
    float val = ((float)arc4random() / ARC4RANDOM_MAX) * range + num1;
    int val1 = val * 10;
    float val2= (float)val1 / 10.0f;
    return val2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a random color. I have done it using brute
I have been trying to get a working algorithm that detects intersection between a
I am trying to generate a random number between two floats (the max is
I'm trying to get a string in my call from an array. But have
i have been trying to get the singer/songwriter title to float to the right
Never had problems with centering divs before but I have been trying to get
I trying to get yahoo prices into sqlite... I have the code below, but
First of all this is homework. I have been trying to get rid of
I'm trying to get rid of floats in my functions. I have one function
I have an annoying CSS layout problem. I'm trying to float images on a

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.