asiftasleem / nbuilder

Automatically exported from code.google.com/p/nbuilder
0 stars 0 forks source link

UniqueRandomGenerator next double with min and max ignores min and max values #67

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
[Test]
public void Next_DoubleWithMinAndMaxOutsideDefaultRange_DoubleBetweenMinAndMax()
{
    IRandomGenerator generator = new RandomGenerator();

    // The default range for .NET's Random.NextDouble is 0.0 to 1.0
    const double min = 1.1;
    const double max = 1.2;

    double actual = generator.Next(min, max);

    Assert.That(actual >= min && actual <= max);
}

What is the expected output? What do you see instead?
A number between min and max.
A number between 0.0 and 1.0.

What version of the product are you using? On what operating system?
r53 on Windows 7.

Please provide any additional information below.
Here's a fix:
public virtual double Next(double min, double max)
{
    if (max <= min) throw new InvalidRangeException();
    double range = max - min;
    double random = rnd.NextDouble();
    double randomWithinRange = range * random;
    return randomWithinRange + min;
}

Original issue reported on code.google.com by pckuj...@gmail.com on 25 Feb 2011 at 6:18

Attachments: