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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:48:13+00:00 2026-05-26T22:48:13+00:00

Intuitively one might write a random double generator as follows: double randDouble(double lowerBound, double

  • 0

Intuitively one might write a random double generator as follows:

double randDouble(double lowerBound, double upperBound)
{
    double range = upperBound - lowerBound;
    return lowerBound + range * rand();
}

Suppose we assume that rand() returns an evenly distributed pseudorandom double on the interval [0, 1).

Is this method guaranteed to return a random double within [lowerBound, upperBound) with a uniform probability distribution? I’m specifically interested in whether the nature of floating point calculations might cause spikes or dips in the final distribution for some ranges.

  • 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-26T22:48:13+00:00Added an answer on May 26, 2026 at 10:48 pm

    First, rand() generates pseudo-random numbers and not truly random. Thus, I will assume you are asking if your function generates pseudo-random numbers within the specified range.

    Second, like Oli Charlesworth said, many rand implementations return a number between 0 and RAND_MAX, where RAND_MAX is the largest possible value it can take. In these cases, you can obtain a value in [0, 1) with

    double r = rand()/((double)RAND_MAX+1);
    

    the +1 is there so that r can’t be 1.

    Other languages have a rand that returns a value between 0 and 1, in which case you don’t need to do the above division. Either way, it turns out that your function returns a decent approximation of a random distribution. See the following link for more details: http://www.thinkage.ca/english/gcos/expl/c/lib/rand.html Note that this link gives you slightly different functions which they claim work a bit better, but the one you have probably works good enough.

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

Sidebar

Related Questions

I have an Class with a unidirectional one to many relationship as follows: public
Most of Knockout seems very intuitive. One thing that is strange to me though
I've been trying to see if I can build my JavaScript objects as intuitively
Consider the following problem. We are given an array of elements belonging to one
This might be a bad idea but I saw this design and want to
Our DBA requires us to return all tabular data from stored procedures in a
This might be a stupid question but if there's a better or proper way
I realize this question might not be that programming related, and that it by
This might be a silly question, but I have never found a satisfying way
Lets suppose I have an .NET client application that connects to a WCF service,

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.