3

in the Documentation Random.NextDouble it is written that

Returns a random number between 0.0 and 1.0.

I want to ask that is it possible that it will return 1 in any case?

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
MYK
  • 825
  • 12
  • 25
  • 2
    I'm not sure I understand the upvotes. Did you read the next sentence of the documentation in that link? – mcalex Dec 25 '12 at 11:58

1 Answers1

15

No - according to the doc, zero is inclusive, but 1 is not:

A double-precision floating point number greater than or equal to 0.0, and less than 1.0.

MSDN

Rotem
  • 21,452
  • 6
  • 62
  • 109
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I can't help wondering if it can return a value between `1E-323` and `1E-17`. One could think it somehow generated a random "mantissa" with a fixed "scale". – Jeppe Stig Nielsen Dec 25 '12 at 12:45
  • Another thing: A number strictly less than `1.0` might display as `1` with the usual string representation. For example `double d = 0.99999999999999978; Console.WriteLine(d < 1.0); /* "True" */ Console.WriteLine(d); /* "1" */` – Jeppe Stig Nielsen Dec 25 '12 at 12:48
  • 1
    @JeppeStigNielsen That can happen in theory, but won't happen with the current implementation of `Random.NextDouble`, since it only uses about 31 or 32 bits instead of the full 53. Doesn't use subnorms either. It's simply something like `randomInt(0..2^31-1)/2^31` – CodesInChaos Dec 25 '12 at 12:59
  • @CodesInChaos Interesting. I was thinking about something like this. I experimented a bit, and it looks like `myRandom.NextDouble() * (Math.Pow(2.0, 31.0) - 1.0)` always gives an integer. However, it is was not easy to detect from `BitConverter.GetBytes(myRandom.NextDouble())` because the factor seems to be not a power of two, but one less than a power of two, [2147483647](http://en.wikipedia.org/wiki/2147483647). – Jeppe Stig Nielsen Dec 25 '12 at 13:27
  • @CodesInChaos Are you looking at the implementation? I think it's strange that the denominator is odd, 2147483647. Is the maximal output of `NextDouble()` then equal to `2147483646.0 / 2147483647.0`, or is the answer we're commenting on incomplete (I mean maybe `1.0 == 2147483647.0 / 2147483647.0` **is** possible)? – Jeppe Stig Nielsen Dec 25 '12 at 13:31
  • @JeppeStigNielsen I don't remember the details, but there was some weirdness in that area. I think `Random.Next()` never returns `int.MaxValue`, but returns `int.MaxValue-1` more often, or something like that. `Random.NextDouble`'s issues also bleed over to `Random.Next(n)` causing significant bias for some `n`. – CodesInChaos Dec 25 '12 at 13:46
  • @CodesInChaos Since 6 divides neither 2147483647 nor 2147483648, one could imagine that they implemented `.Next(6)` in a way that distributes the 2147483647 outcomes unevenly among 0, 1, 2, ..., 5. They probably just use integer division by 6. If that's their way, consider `.Next(2000000000)`. The lowest values, 0 to 147483646, would have _double_ probability compared to the remaining values, 147483647 to 1999999999. If that's the case, maybe the empirical mean value of a lot of `.Next(2000000000)` numbers will deviate from the expected result? – Jeppe Stig Nielsen Dec 25 '12 at 15:24
  • 1
    @Jeppe They must have implemented `Next(n)` as something like `NextDouble()*n`. That causes a huge bias for some `n`, such as [`1431655765`](http://stackoverflow.com/a/6842191/445517). – CodesInChaos Dec 25 '12 at 15:32
  • I wanted to add a note because I had to hunt a bug which was related to this. NextDouble may not return 1, but casting the result to float CAN return 1. – KakCAT Jul 08 '20 at 13:54