Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random double between min and max

Tags:

c++

random

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?

like image 490
David Gomes Avatar asked May 27 '12 17:05

David Gomes


People also ask

How do you generate a random double?

In order to generate Random double type numbers in Java, we use the nextDouble() method of the java. util. Random class. This returns the next random double value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.

How do you generate a random number with MIN MAX?

The built-in function Math. random() creates a random value from 0 to 1 (not including 1 ). Write the function random(min, max) to generate a random floating-point number from min to max (not including max ).

What is random NextDouble in C#?

The Random. NextDouble() method in C# is used to return a random floating-point number that is greater than or equal to 0.0, and less than 1.0.

Is it possible to generate a random number between 2 doubles?

Is it possible to generate a random number between 2 doubles? public double GetRandomeNumber (double minimum, double maximum) { return Random.NextDouble (minimum, maximum) } Any thoughts would be appreciated. Yes. Random.NextDouble returns a double between 0 and 1.

How to get random number between 0 and 1 in JavaScript?

Random.NextDouble returns a double between 0 and 1. You then multiply that by the range you need to go into (difference between maximum and minimum) and then add that to the base (minimum). public double GetRandomNumber (double minimum, double maximum) { Random random = new Random (); return random.NextDouble () * (maximum - minimum) + minimum; }

How to generate a random number in Java?

public double GetRandomNumber(double minimum, double maximum) { Random random = new Random(); return random.NextDouble() * (maximum - minimum) + minimum; }. Real code should have random be a static member. This will save the cost of creating the random number generator, and will enable you to call GetRandomNumber very frequently.

How does the random class generate pseudo random numbers?

Well, the Random class actually generates pseudo random numbers, with the “seed” for the randomizer being the system time. If your loop is sufficiently fast, the system clock time will not appear different to the randomizer and each new instance of the Random class would start off with the same seed and give you the same pseudo random number.


2 Answers

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.
like image 101
Luchian Grigore Avatar answered Oct 31 '22 20:10

Luchian Grigore


You can use this for generating random double or floating numbers:

((double) rand()*(max-min)/(double)RAND_MAX-min);
like image 39
user2514807 Avatar answered Oct 31 '22 19:10

user2514807