Mark Gerolimatos
Mark Gerolimatos

Reputation: 2584

What is the range of Random.nextDouble in Scala?

The Scaladoc page for Random ( http://www.scala-lang.org/api/current/scala/util/Random.html ) specifies that nextDouble "Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence."

Pretty glaringly, it leaves out "inclusion". For instance, are value 0.0 and 1.0 possible? If so, which one (or both)? For instance, if I am dividing by this number, I'd really want to make sure that 0 was not a returned value. In other cases, I might not want 1.0. Quite obviously, this is hard to get via testing, as there about 10^15 values between 0.0 and 1.0.

Which is it? Please note, since not everyone can remember which of "(" and "[" means "inclusive", please just say "includes X".

Upvotes: 9

Views: 6934

Answers (1)

themel
themel

Reputation: 8895

This is due to the fact that scala.util.Random is a very thin wrapper around java.util.Random (see the source), which is documented in sufficient detail:

The general contract of nextDouble is that one double value, chosen (approximately) uniformly from the range 0.0d (inclusive) to 1.0d (exclusive), is pseudorandomly generated and returned.

Upvotes: 15

Related Questions