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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:07:43+00:00 2026-06-04T21:07:43+00:00

double get_random(double min, double max) { /* Returns a random double between min and

  • 0
double get_random(double min, double max) {
  /* Returns a random double between min and max */

  return min * ((double) rand() / (double) RAND_MAX) - max;
}

That’s my function to generate random doubles between a min and a max. However, when I call get_random(-1.0, 1.0);, I get values between -2.0 and -1.0.

Any idea of what I’m doing wrong and how I can fix it?

  • 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-04T21:07:44+00:00Added an answer on June 4, 2026 at 9:07 pm

    Shouldn’t the formula be

    (max - min) * ( (double)rand() / (double)RAND_MAX ) + min
    
    • (double)rand() / (double)RAND_MAX returns a random number between 0 and 1
    • (max - min) * ( (double)rand() / (double)RAND_MAX ) returns a random number between 0 and max - min.
    • the whole expression will return a random number between 0 + min and min + (max-min) – i.e. min and max.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say that I would like to get an random double value wich is:
I have a program that generates a single random character, using the randomCharacter function,
How do you get random Double values between 0.0 and 0.06 in Java?
I am calling arc4random in a function in my iOS application to generate random
Snippets: private double memberVal; public double MemberVal { get { return memberVal; } set
Suppose I wish to have 2 functions, one that generates a random integer within
Here's my test: @Test public void randomDouble() { int Min = -1; int Max
Above is an example image generated by using rand() to get random coordinates and
I am using the boost::random to generate random velocity values and I want to
i want to generate an array of random numbers for example if the range

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.