0

Possible Duplicate:
Random Number Between 2 Double Numbers

I want to generate a number between -8.000 and -3100.000.

How can this be achieved?

Whats the most efficient code?

Community
  • 1
  • 1
DeadlyDagger
  • 599
  • 4
  • 12
  • 28

2 Answers2

6

Here's the standard way to produce a random number within a range. Note that I'm producing several to illustrate that you don't want to create a new RNG for each one when using a pseudorandom number generator to avoid situations where they can have the same seed value.

var rng = new Random();
var randoms = new double[10];
double MIN_VALUE = -3100.0;
double MAX_VALUE = -8.0;

for (var int i = 0; i < randoms.Length; ++i)
{
    randoms[i] = rng.NextDouble() * (MAX_VALUE - MIN_VALUE) + MIN_VALUE;
}
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
0

Pretty standard answer one will get after asking this question is to do

Random rnd = new Random();
double MIN_VALUE = -3100.0;
double MAX_VALUE = -8.0;

double random_value = rnd.NextDouble() * (MAX_VALUE - MIN_VALUE) + MIN_VALUE;

However, I'm afraid that's not 100% correct. Ok, it depends on what you mean by "a number between". And here's why:

Random.NextDouble Method returns "a double-precision floating point number greater than or equal to 0.0, and less than 1.0." (as described in MSDN Library)

So, since rnd.NextDouble() returns a number in range [0; 1), when you do

rnd.NextDouble() * (MAX_VALUE - MIN_VALUE) + MIN_VALUE

you will get a random number in range [MIN_VALUE; MAX_VALUE) (MAX_VALUE exclusively). If that's what you wanted, then great. But what if you wanted a random number in range [MIN_VALUE; MAX_VALUE] (MAX_VALUE inclusively - which I believe you did)?

Well, I don't know how to do that with double numbers of great precision. So, I'm gonna assume that when you wrote "-8.000" and "-3100.000" what you meant by that was that you want a 0.001 precision in the randomly generated number. This is how I would try to achieve that:

Random rnd = new Random();
double MIN_VALUE = -3100.0;
double MAX_VALUE = -8.0;
int PRECISION = 1000;

int min = (int)(MIN_VALUE * PRECISION);    //I know these could be sent as arguments to rnd.Next(),
int max = (int)(MAX_VALUE * PRECISION);    //but I wanted to keep it easier to read and understand

double random_value = rnd.Next(min, max + 1);    //[-3100000; -8000]
random_value /= PRECISION;

That would give you a random number in a range [-3100.000; -8.000].

If I would use rnd.Next(min, max) then the result would be in range [-3100.000; -8.001], and that's because Random.Next Method (Int32, Int32) returns "A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue." (see Random.Next Method (Int32, Int32).

And last thing, if you're using rnd.NextDouble() method, and you have to generate huge amount of random numbers within a range [Min, Max], and you're doing it in a loop, you might want to consider taking MAX_VALUE - MIN_VALUE out of the loop (or embedded loops). No need to calculate this every single time.

double MAX_MINUS_MIN = MAX_VALUE - MIN_VALUE;

for (int i = 0; i < BIG_SCARY_NUMBER; ++i)
    for (int j = 0; j < BIG_SCARY_NUMBER; ++j)
        for (int k = 0; k < BIG_SCARY_NUMBER; ++k)
            randoms[i, j, k] = rnd.NextDouble() * MAX_MINUS_MIN + MIN_VALUE;
beam022
  • 1,793
  • 4
  • 20
  • 27