Random Number Between 2 Double Numbers

Random Number Between 2 Double Numbers

Yes.

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;
}

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. Since we are initializing a new RNG with every call, if you call quick enough that the system time doesn't change between calls the RNG will get seeded with the exact same timestamp, and generate the same stream of random numbers.

Random double between given numbers

You can easily define a method that returns a random number between two values:

private static readonly Random random = new Random();

private static double RandomNumberBetween(double minValue, double maxValue)
{
var next = random.NextDouble();

return minValue + (next * (maxValue - minValue));
}

You can then call this method with your desired values:

RandomNumberBetween(1.41421, 3.14159)

Get random number between two doubles

This works for me :

Sub Bouton1_Cliquer()
Dim r As Double
Randomize
r = (35.9 - 37.2) * Rnd + 37.2
End Sub

1000 Values Test

generate random double numbers in c++

Here's how

double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}

Remember to call srand() with a proper seed each time your program starts.

[Edit]
This answer is obsolete since C++ got it's native non-C based random library (see Alessandro Jacopsons answer)
But, this still applies to C

How to generate a random double number in the inclusive [0,1] range?

After taking a shower, I have conceived of a potential solution based on my understanding of how a random floating point generator works. My solution makes three assumptions, which I believe to be reasonable, however I can not verify if these assumptions are correct or not. Because of this, the following code is purely academic in nature, and I would not recommend its use in practice. The assumptions are as follows:

  1. The distribution of random.NextDouble() is uniform
  2. The difference between any two adjacent numbers in the range produced by random.NextDouble() is a constant epsilon e
  3. The maximum value generated by random.NextDouble() is equal to 1 - e

Provided that those three assumptions are correct, the following code generates random doubles in the range [0, 1].

// For the sake of brevity, we'll omit the finer details of reusing a single instance of Random
var random = new Random();

double RandomDoubleInclusive() {
double d = 0.0;
int i = 0;

do {
d = random.NextDouble();
i = random.Next(2);
} while (i == 1 && d > 0)

return d + i;
}

This is somewhat difficult to conceptualize, but the essence is somewhat like the below coin-flipping explanation, except instead of a starting value of 0.5, you start at 1, and if at any point the sum exceeds 1, you restart the entire process.

From an engineering standpoint, this code is a blatant pessimization with little practical advantage. However, mathematically, provided that the original assumptions are correct, the result will be as mathematically sound as the original implementation.

Below is the original commentary on the nature of random floating point values and how they're generated.

Original Reply:

Your question carries with it a single critical erroneous assumption: Your use of the word "Correct". We are working with floating point numbers. We abandoned correctness long ago.

What follows is my crude understanding of how a random number generator produces a random floating point value.

You have a coin, a sum starting at zero, and a value starting at one half (0.5).

  1. Flip the coin.
  2. If heads, add the value to the sum.
  3. Half the value.
  4. Repeat 23 times.

You have just generated a random number. Here are some properties of the number (for reference, 2^23 is 8,388,608, and 2^(-23) is the inverse of that, or approximately 0.0000001192):

  • The number is one of 2^23 possible values
  • The lowest value is 0
  • The highest value is 1 - 2^(-23);
  • The smallest difference between any two potential values is 2^(-23)
  • The values are evenly distributed across the range of potential values
  • The odds of getting any one value are completely uniform across the range
  • Those last two points are true regardless of how many times you flip the coin
  • The process for generating the number was really really easy

That last point is the kicker. It means if you can generate raw entropy (i.e. perfectly uniform random bits), you can generate an arbitrarily precise number in a very useful range with complete uniformity. Those are fantastic properties to have. The only caveat is that it doesn't generate the number 1.

The reason that caveat is seen as acceptable is because every other aspect of the generation is so damned good. If you're trying to get a high precision random value between 0 and 1, chances are you don't actually care about landing on 1 any more than you care about landing on 0.38719, or any other random number in that range.

While there are methods for getting 1 included in your range (which others have stated already), they're all going to cost you in either speed or uniformity. I'm just here to tell you that it might not actually be worth the tradeoff.

c# Random number between Double.MinValue and Double.MaxValue

Consider changing the logic of how you calculate the random double to something like

private static Double Range(Double minValue, Double maxValue)
{
var x = random.NextDouble();

return x * maxValue + (1 - x) * minValue;
}

The reason your one fails is because double.MinValue would be negative, thus if you do maxValue - minValue you are essentially doing double.MaxValue * 2.

C# generating random double number range

You can write an extension method to Random for Random.NextDouble(double MinValue,double MaxValue) so that you can use it everywhere:

public static class RandomExtensions
{
public static double NextDouble(this Random RandGenerator, double MinValue, double MaxValue)
{
return RandGenerator.NextDouble() * (MaxValue - MinValue) + MinValue;
}
}


Related Topics



Leave a reply



Submit