From 11c887988dba925bf0f724e98478512caea3b0da Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 27 Sep 2020 20:44:27 +0200 Subject: [PATCH] WIP: Fixing normal distribution --- FastRng/Double/Distributions/Normal.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/FastRng/Double/Distributions/Normal.cs b/FastRng/Double/Distributions/Normal.cs index b7b4035..3326ddc 100644 --- a/FastRng/Double/Distributions/Normal.cs +++ b/FastRng/Double/Distributions/Normal.cs @@ -29,13 +29,19 @@ namespace FastRng.Double.Distributions if (this.Random == null) return double.NaN; - var u1 = await this.Random.GetUniform(token); - var u2 = await this.Random.GetUniform(token); - var r = Math.Sqrt(-2.0 * Math.Log(u1)); - var theta = 2.0 * Math.PI * u2; - var value = r * Math.Sin(theta); - - return this.Mean + this.StandardDeviation * value; + // + // Previously: + // + // var u1 = await this.Random.GetUniform(token); + // var u2 = await this.Random.GetUniform(token); + // var r = Math.Sqrt(-2.0 * Math.Log(u1)); + // var theta = 2.0 * Math.PI * u2; + // var value = r * Math.Sin(theta); + // return this.Mean + this.StandardDeviation * value; + + const double SQRT_2PI = 2.506628275; + var x = await this.Random.GetUniform(token); // BUG: It seems, that uniform is not uniform (enough) or RunningStatistics had specific issues + return 1.0 / (this.StandardDeviation * SQRT_2PI) * Math.Exp(-0.5 * Math.Pow((x - this.Mean) / this.StandardDeviation, 2.0)); } } } \ No newline at end of file