简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM