2

I have searched google through and no answer yet.

So question is, I need to create random generator API as following:

public static Double Range(Double minValue, Double maxValue)
{
     return random.NextDouble() * (maxValue - minValue) + minValue; // random is just System.Random
}

everything works fine until I put Double.MinValue and Double.MaxValue into parameters, since it generates "infinite" invalid double (since the operation produces value out of 64bit double range).

Is there a way how to write the code so it can process and generate valid double even when Double.MinValue and Double.MaxValue are used?

Wrymn
  • 173
  • 1
  • 2
  • 13
  • Consider calling random twice - once to get the value, the other for the sign. Then `return random.NextDouble() * double.MaxValue * sign;` Note this may not necessarily return `MaxValue` and `MinValue` themselves - but it will get pretty close. _Note this may, or may not, give you the distribution of values you are hoping for._ – mjwills Jan 26 '21 at 12:00
  • 1
    Even if you make that work, it won't give the results you expect, since the range is to broad. Can your code really work with random numbers both incredibly small (-1E100) very tiny (1E-100) or really big (1E100)? – PMF Jan 26 '21 at 12:01

1 Answers1

5

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.

Febre
  • 68
  • 4
  • This code always produces the same result of: 1.79769313486232E+308, so it does not seem to be correct. – Wrymn Jan 26 '21 at 12:36
  • @Zer0 I did print it in debug using ordinary System.Console.WriteLine() without any formatting. Its quite strange we get different results. – Wrymn Jan 26 '21 at 12:49
  • @Wrymn I just re-ran on a longer set. Distribution looks bizarre, and not what I'd expect. Unsure why I'm getting postive and negative looking random numbers. But they do look bad. I can chalk it up to normally looking at "cryptographic random", which `Random` doesn't guarantee. Math seems right though. – Zer0 Jan 26 '21 at 12:51
  • 2
    You don't put a `new Random()` inside a method like that, you pass it as a parameter. – xanatos Jan 26 '21 at 12:51
  • 1
    You shouldn't create a `Random` instance each time you call the method, otherwise this will happen: [Random.Next returns always the same values](https://stackoverflow.com/questions/1654887/random-next-returns-always-the-same-values) – Theodor Zoulias Jan 26 '21 at 12:52
  • 1
    Okay seems to work now for some reason. And yes, as @TheodorZoulias said, do not put new Random() inside the method, just create it once and cache it. – Wrymn Jan 26 '21 at 12:56
  • @xanatos Thanks and yes I do agree, I have updated my answer. – Febre Jan 26 '21 at 14:10
  • @Febre Yes but you have to add `Random random` as a parameter – xanatos Jan 26 '21 at 14:18
  • @xanatos That depends on how your class looks like, you can also have a `static readonly Random random`. I left the new Random out to keep is as close to the method from the OP. – Febre Jan 26 '21 at 14:37