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 8837837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:49:48+00:00 2026-06-14T09:49:48+00:00

I can’t find any solution to generate a random float number in the range

  • 0

I can’t find any solution to generate a random float number in the range of [0,a], where a is some float defined by a user.

I have tried the following, but it doesn’t seem to work correctly.

float x=(float)rand()/((float)RAND_MAX/a)
  • 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-06-14T09:49:49+00:00Added an answer on June 14, 2026 at 9:49 am

    Try:

    float x = (float)rand()/(float)(RAND_MAX/a);
    

    To understand how this works consider the following.

    N = a random value in [0..RAND_MAX] inclusively.
    

    The above equation (removing the casts for clarity) becomes:

    N/(RAND_MAX/a)
    

    But division by a fraction is the equivalent to multiplying by said fraction’s reciprocal, so this is equivalent to:

    N * (a/RAND_MAX)
    

    which can be rewritten as:

    a * (N/RAND_MAX)
    

    Considering N/RAND_MAX is always a floating point value between 0.0 and 1.0, this will generate a value between 0.0 and a.

    Alternatively, you can use the following, which effectively does the breakdown I showed above. I actually prefer this simply because it is clearer what is actually going on (to me, anyway):

    float x = ((float)rand()/(float)(RAND_MAX)) * a;
    

    Note: the floating point representation of a must be exact or this will never hit your absolute edge case of a (it will get close). See this article for the gritty details about why.

    Sample

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
        srand((unsigned int)time(NULL));
    
        float a = 5.0;
        for (int i=0;i<20;i++)
            printf("%f\n", ((float)rand()/(float)(RAND_MAX)) * a);
        return 0;
    }
    

    Output

    1.625741
    3.832026
    4.853078
    0.687247
    0.568085
    2.810053
    3.561830
    3.674827
    2.814782
    3.047727
    3.154944
    0.141873
    4.464814
    0.124696
    0.766487
    2.349450
    2.201889
    2.148071
    2.624953
    2.578719
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Can anybody tell me a regular expression to use within some PHP to find
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can I order my users in the database, so I don't have to say
Can any one tell, how to get the result of LINQ query contains group
Can anyone explain to me why this program: for(float i = -1; i <
I have a jquery bug and I've been looking for hours now, I can't
Can anyone suggest how to underline the title of a UIButton ? I have

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.