summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dotnet-library/scala/util/Random.scala25
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.