diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2009-05-28 14:06:24 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2009-05-28 14:06:24 +0000 |
commit | b7eac378dadaac07aa77a9acfc8a64b212c09e83 (patch) | |
tree | 721a032b39e69d70b4b4ed6192be475ce0f7fa45 | |
parent | 13f7432497774a2e53eecd157c4e1f2d5ad52fd7 (diff) | |
download | scala-b7eac378dadaac07aa77a9acfc8a64b212c09e83.tar.gz scala-b7eac378dadaac07aa77a9acfc8a64b212c09e83.tar.bz2 scala-b7eac378dadaac07aa77a9acfc8a64b212c09e83.zip |
Implements feature request #2008.
-rw-r--r-- | src/dotnet-library/scala/util/Random.scala | 23 | ||||
-rw-r--r-- | src/library/scala/util/Random.scala | 3 |
2 files changed, 23 insertions, 3 deletions
diff --git a/src/dotnet-library/scala/util/Random.scala b/src/dotnet-library/scala/util/Random.scala index 3235464a4f..726f35478f 100644 --- a/src/dotnet-library/scala/util/Random.scala +++ b/src/dotnet-library/scala/util/Random.scala @@ -51,7 +51,28 @@ class Random(self0: System.Random) { * double value with mean 0.0 and standard deviation 1.0 from this * random number generator's sequence. */ - //def nextGaussian(): Double + def nextGaussian(): Double = synchronized { + if (nextGaussianAvailable) { + nextGaussianAvailable = false + nextGaussian + } else { + var gauss1: Double + var gauss2: Double + var s: Double + 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) + } while (s >= 1 || s == 0); + val multiplier = Math.sqrt(-2 * Math.log(s)/s) + nextGaussian = gauss1 * multiplier + nextGaussianAvailable = true + gauss2 * multiplier + } + } + + private var nextGaussianAvailable: Boolean = false + private var nextGaussian: Double = _ /** Returns the next pseudorandom, uniformly distributed int value * from this random number generator's sequence. diff --git a/src/library/scala/util/Random.scala b/src/library/scala/util/Random.scala index 482f4e7c42..09d15ff500 100644 --- a/src/library/scala/util/Random.scala +++ b/src/library/scala/util/Random.scala @@ -50,8 +50,7 @@ class Random(val self: java.util.Random) { * double value with mean 0.0 and standard deviation 1.0 from this * random number generator's sequence. */ - // XXX why is this the only method of java.util.Random to be commented out? - //def nextGaussian(): Double = self.nextGaussian() + def nextGaussian(): Double = self.nextGaussian() /** Returns the next pseudorandom, uniformly distributed int value * from this random number generator's sequence. |