Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the range of Random.nextDouble in Scala?

Tags:

random

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".

like image 838
Mark Gerolimatos Avatar asked Oct 09 '12 16:10

Mark Gerolimatos


People also ask

Is Random nextDouble inclusive?

Gets the next random Double value uniformly distributed between 0 (inclusive) and 1 (exclusive).

How do you get a Random double?

In order to generate Random double type numbers in Java, we use the nextDouble() method of the java. util. Random class. This returns the next random double value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.


1 Answers

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.

like image 97
themel Avatar answered Sep 21 '22 16:09

themel