diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2009-05-29 12:55:09 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2009-05-29 12:55:09 +0000 |
commit | 09dc46783dcb9c94dc1e3953c5689fd594d484f8 (patch) | |
tree | cdfe8bc652ec720c491edb581ed2b3b3d08bd76b /src/dotnet-library | |
parent | 08c63fc9a1b8feb13fe80ddcd9c56f6fe71f1914 (diff) | |
download | scala-09dc46783dcb9c94dc1e3953c5689fd594d484f8.tar.gz scala-09dc46783dcb9c94dc1e3953c5689fd594d484f8.tar.bz2 scala-09dc46783dcb9c94dc1e3953c5689fd594d484f8.zip |
Fixed "scala.util.Random" in MSIL library.
Diffstat (limited to 'src/dotnet-library')
-rw-r--r-- | src/dotnet-library/scala/util/Random.scala | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/dotnet-library/scala/util/Random.scala b/src/dotnet-library/scala/util/Random.scala index 726f35478f..abfc654ce2 100644 --- a/src/dotnet-library/scala/util/Random.scala +++ b/src/dotnet-library/scala/util/Random.scala @@ -50,29 +50,32 @@ class Random(self0: System.Random) { /** Returns the next pseudorandom, Gaussian ("normally") distributed * double value with mean 0.0 and standard deviation 1.0 from this * random number generator's sequence. + * This implements the Box-Muller transformation on the pseudorandom + * `nextDouble` method of this class, and should be equivalent to + * the implementation of `nextGaussian` in the Java API. */ def nextGaussian(): Double = synchronized { if (nextGaussianAvailable) { nextGaussianAvailable = false - nextGaussian + nextGaussianCache } else { - var gauss1: Double - var gauss2: Double - var s: Double + var u: Double = 0.0 + var v: Double = 0.0 + var s: Double = 0.0 do { - gauss1 = 2 * nextDouble() - 1 // between -1.0 and 1.0 - gauss2 = 2 * nextDouble() - 1 // between -1.0 and 1.0 - s = Math.pow(gauss1) + Math.pow(gauss2) + u = 2 * nextDouble() - 1 // [-1, +1] + v = 2 * nextDouble() - 1 // [-1, +1] + s = Math.pow(u, 2) + Math.pow(v, 2) } while (s >= 1 || s == 0); - val multiplier = Math.sqrt(-2 * Math.log(s)/s) - nextGaussian = gauss1 * multiplier + val mul = Math.sqrt((-2 * Math.log(s)) / s) + nextGaussianCache = u * mul nextGaussianAvailable = true - gauss2 * multiplier + v * mul } } private var nextGaussianAvailable: Boolean = false - private var nextGaussian: Double = _ + private var nextGaussianCache: Double = _ /** Returns the next pseudorandom, uniformly distributed int value * from this random number generator's sequence. |