From 44abcc48b6ffd0e63dd9ec044aa510ae285d4b26 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 7 Nov 2020 15:12:56 +0100 Subject: [PATCH] Refactored setting IRandom to distribution --- FastRng/Double/Distributions/BetaA2B2.cs | 4 + FastRng/Double/Distributions/BetaA2B5.cs | 4 + FastRng/Double/Distributions/BetaA5B2.cs | 4 + .../Double/Distributions/CauchyLorentzX0.cs | 4 + .../Double/Distributions/CauchyLorentzX1.cs | 4 + FastRng/Double/Distributions/ChiSquareK1.cs | 4 + FastRng/Double/Distributions/ChiSquareK10.cs | 4 + FastRng/Double/Distributions/ChiSquareK4.cs | 4 + FastRng/Double/Distributions/Distribution.cs | 29 +++---- .../Double/Distributions/ExponentialLa10.cs | 4 + .../Double/Distributions/ExponentialLa5.cs | 4 + FastRng/Double/Distributions/GammaA5B15.cs | 4 + FastRng/Double/Distributions/IDistribution.cs | 2 - .../Distributions/InverseExponentialLa10.cs | 4 + .../Distributions/InverseExponentialLa5.cs | 4 + .../Double/Distributions/InverseGammaA3B05.cs | 4 + FastRng/Double/Distributions/LaplaceB01M0.cs | 4 + FastRng/Double/Distributions/LaplaceB01M05.cs | 4 + FastRng/Double/Distributions/LogNormalS1M0.cs | 4 + FastRng/Double/Distributions/NormalS02M05.cs | 4 + FastRng/Double/Distributions/StudentTNu1.cs | 4 + FastRng/Double/Distributions/Uniform.cs | 13 ++- FastRng/Double/Distributions/WeibullK05La1.cs | 4 + FastRng/Double/MultiThreadedRng.cs | 6 -- FastRng/Double/ShapeFitter.cs | 3 +- FastRng/Float/Distributions/BetaA2B2.cs | 4 + FastRng/Float/Distributions/BetaA2B5.cs | 4 + FastRng/Float/Distributions/BetaA5B2.cs | 4 + .../Float/Distributions/CauchyLorentzX0.cs | 4 + .../Float/Distributions/CauchyLorentzX1.cs | 4 + FastRng/Float/Distributions/ChiSquareK1.cs | 4 + FastRng/Float/Distributions/ChiSquareK10.cs | 4 + FastRng/Float/Distributions/ChiSquareK4.cs | 4 + FastRng/Float/Distributions/Distribution.cs | 29 +++---- .../Float/Distributions/ExponentialLa10.cs | 4 + FastRng/Float/Distributions/ExponentialLa5.cs | 4 + FastRng/Float/Distributions/GammaA5B15.cs | 4 + FastRng/Float/Distributions/IDistribution.cs | 2 - .../Distributions/InverseExponentialLa10.cs | 4 + .../Distributions/InverseExponentialLa5.cs | 4 + .../Float/Distributions/InverseGammaA3B05.cs | 4 + FastRng/Float/Distributions/LaplaceB01M0.cs | 4 + FastRng/Float/Distributions/LaplaceB01M05.cs | 4 + FastRng/Float/Distributions/LogNormalS1M0.cs | 4 + FastRng/Float/Distributions/NormalS02M05.cs | 4 + FastRng/Float/Distributions/StudentTNu1.cs | 4 + FastRng/Float/Distributions/Uniform.cs | 13 ++- FastRng/Float/Distributions/WeibullK05La1.cs | 4 + FastRng/Float/MultiThreadedRng.cs | 6 -- FastRng/Float/ShapeFitter.cs | 3 +- FastRngTests/Double/Distributions/BetaA2B2.cs | 38 ++------- FastRngTests/Double/Distributions/BetaA2B5.cs | 38 ++------- FastRngTests/Double/Distributions/BetaA5B2.cs | 38 ++------- .../Double/Distributions/CauchyLorentzX0.cs | 40 +++------- .../Double/Distributions/CauchyLorentzX1.cs | 40 +++------- .../Double/Distributions/ChiSquareK1.cs | 39 ++------- .../Double/Distributions/ChiSquareK10.cs | 39 ++------- .../Double/Distributions/ChiSquareK4.cs | 39 ++------- .../Double/Distributions/ExponentialLa10.cs | 36 ++------- .../Double/Distributions/ExponentialLa5.cs | 36 ++------- .../Double/Distributions/GammaA5B15.cs | 38 ++------- .../Distributions/InverseExponentialLa10.cs | 36 ++------- .../Distributions/InverseExponentialLa5.cs | 36 ++------- .../Double/Distributions/InverseGammaA3B05.cs | 38 ++------- .../Double/Distributions/LaplaceB01M0.cs | 38 ++------- .../Double/Distributions/LaplaceB01M05.cs | 38 ++------- .../Double/Distributions/LogNormalS1M0.cs | 38 ++------- .../Double/Distributions/NormalS02M05.cs | 38 ++------- .../Double/Distributions/StudentTNu1.cs | 38 ++------- FastRngTests/Double/Distributions/Uniform.cs | 64 ++++++--------- .../Double/Distributions/WeibullK05La1.cs | 38 ++------- FastRngTests/Double/MultiThreadedRngTests.cs | 79 ++++++++----------- FastRngTests/Double/PerformanceTests.cs | 5 +- FastRngTests/Float/Distributions/BetaA2B2.cs | 41 +++------- FastRngTests/Float/Distributions/BetaA2B5.cs | 41 +++------- FastRngTests/Float/Distributions/BetaA5B2.cs | 40 +++------- .../Float/Distributions/CauchyLorentzX0.cs | 40 +++------- .../Float/Distributions/CauchyLorentzX1.cs | 40 +++------- .../Float/Distributions/ChiSquareK1.cs | 41 +++------- .../Float/Distributions/ChiSquareK10.cs | 41 +++------- .../Float/Distributions/ChiSquareK4.cs | 41 +++------- .../Float/Distributions/ExponentialLa10.cs | 36 ++------- .../Float/Distributions/ExponentialLa5.cs | 38 +++------ .../Float/Distributions/GammaA5B15.cs | 38 ++------- .../Distributions/InverseExponentialLa10.cs | 36 ++------- .../Distributions/InverseExponentialLa5.cs | 36 ++------- .../Float/Distributions/InverseGammaA3B05.cs | 38 ++------- .../Float/Distributions/LaplaceB01M0.cs | 38 ++------- .../Float/Distributions/LaplaceB01M05.cs | 38 ++------- .../Float/Distributions/LogNormalS1M0.cs | 38 ++------- .../Float/Distributions/NormalS02M05.cs | 40 +++------- .../Float/Distributions/StudentTNu1.cs | 40 +++------- FastRngTests/Float/Distributions/Uniform.cs | 66 +++++++--------- .../Float/Distributions/WeibullK05La1.cs | 38 ++------- FastRngTests/Float/MultiThreadedRngTests.cs | 79 ++++++++----------- FastRngTests/Float/PerformanceTests.cs | 4 +- 96 files changed, 664 insertions(+), 1437 deletions(-) diff --git a/FastRng/Double/Distributions/BetaA2B2.cs b/FastRng/Double/Distributions/BetaA2B2.cs index e8dd98d..5ae8cbb 100644 --- a/FastRng/Double/Distributions/BetaA2B2.cs +++ b/FastRng/Double/Distributions/BetaA2B2.cs @@ -10,6 +10,10 @@ namespace FastRng.Double.Distributions private const double BETA = 2; private const double CONSTANT = 4; + public BetaA2B2(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * Math.Pow(x, ALPHA - 1) * Math.Pow(1 - x, BETA - 1); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/BetaA2B5.cs b/FastRng/Double/Distributions/BetaA2B5.cs index 6290e33..ea21505 100644 --- a/FastRng/Double/Distributions/BetaA2B5.cs +++ b/FastRng/Double/Distributions/BetaA2B5.cs @@ -10,6 +10,10 @@ namespace FastRng.Double.Distributions private const double BETA = 5; private const double CONSTANT = 12.2; + public BetaA2B5(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * Math.Pow(x, ALPHA - 1) * Math.Pow(1 - x, BETA - 1); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/BetaA5B2.cs b/FastRng/Double/Distributions/BetaA5B2.cs index 49d289a..b460a8f 100644 --- a/FastRng/Double/Distributions/BetaA5B2.cs +++ b/FastRng/Double/Distributions/BetaA5B2.cs @@ -10,6 +10,10 @@ namespace FastRng.Double.Distributions private const double BETA = 2; private const double CONSTANT = 12.2; + public BetaA5B2(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * Math.Pow(x, ALPHA - 1) * Math.Pow(1 - x, BETA - 1); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/CauchyLorentzX0.cs b/FastRng/Double/Distributions/CauchyLorentzX0.cs index 43a4abd..fa638ae 100644 --- a/FastRng/Double/Distributions/CauchyLorentzX0.cs +++ b/FastRng/Double/Distributions/CauchyLorentzX0.cs @@ -10,6 +10,10 @@ namespace FastRng.Double.Distributions private const double SCALE = 0.1; private const double MEDIAN = 0.0; + public CauchyLorentzX0(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * (1.0 / (Math.PI * SCALE)) * ((SCALE * SCALE) / (Math.Pow(x - MEDIAN, 2) + (SCALE * SCALE))); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/CauchyLorentzX1.cs b/FastRng/Double/Distributions/CauchyLorentzX1.cs index 64a601c..bd8b120 100644 --- a/FastRng/Double/Distributions/CauchyLorentzX1.cs +++ b/FastRng/Double/Distributions/CauchyLorentzX1.cs @@ -10,6 +10,10 @@ namespace FastRng.Double.Distributions private const double SCALE = 0.1; private const double MEDIAN = 1.0; + public CauchyLorentzX1(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * (1.0 / (Math.PI * SCALE)) * ((SCALE * SCALE) / (Math.Pow(x - MEDIAN, 2) + (SCALE * SCALE))); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/ChiSquareK1.cs b/FastRng/Double/Distributions/ChiSquareK1.cs index 41723df..92fb17a 100644 --- a/FastRng/Double/Distributions/ChiSquareK1.cs +++ b/FastRng/Double/Distributions/ChiSquareK1.cs @@ -20,6 +20,10 @@ namespace FastRng.Double.Distributions DIVISOR = twoToTheKHalf * gammaKHalf; } + public ChiSquareK1(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * ((Math.Pow(x, K_HALF_MINUS_ONE) * Math.Exp(-x * 0.5d)) / DIVISOR); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/ChiSquareK10.cs b/FastRng/Double/Distributions/ChiSquareK10.cs index c91b4df..b6dbf62 100644 --- a/FastRng/Double/Distributions/ChiSquareK10.cs +++ b/FastRng/Double/Distributions/ChiSquareK10.cs @@ -19,6 +19,10 @@ namespace FastRng.Double.Distributions var gammaKHalf = MathTools.Gamma(K_HALF); DIVISOR = twoToTheKHalf * gammaKHalf; } + + public ChiSquareK10(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => CONSTANT * ((Math.Pow(x, K_HALF_MINUS_ONE) * Math.Exp(-x * 0.5d)) / DIVISOR); } diff --git a/FastRng/Double/Distributions/ChiSquareK4.cs b/FastRng/Double/Distributions/ChiSquareK4.cs index 7585587..9f5aff4 100644 --- a/FastRng/Double/Distributions/ChiSquareK4.cs +++ b/FastRng/Double/Distributions/ChiSquareK4.cs @@ -19,6 +19,10 @@ namespace FastRng.Double.Distributions var gammaKHalf = MathTools.Gamma(K_HALF); DIVISOR = twoToTheKHalf * gammaKHalf; } + + public ChiSquareK4(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => CONSTANT * ((Math.Pow(x, K_HALF_MINUS_ONE) * Math.Exp(-x * 0.5d)) / DIVISOR); } diff --git a/FastRng/Double/Distributions/Distribution.cs b/FastRng/Double/Distributions/Distribution.cs index f96b6ae..1eadbcd 100644 --- a/FastRng/Double/Distributions/Distribution.cs +++ b/FastRng/Double/Distributions/Distribution.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; @@ -5,30 +6,20 @@ namespace FastRng.Double.Distributions { public abstract class Distribution : IDistribution { - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random + private readonly ShapeFitter fitter; + private readonly IRandom random; + + protected Distribution(IRandom rng) { - get => this.random; - set - { - if(this.random != null) - return; + if (rng == null) + throw new ArgumentNullException(nameof(rng), "An IRandom implementation is needed."); - this.random = value; - this.fitter = new ShapeFitter(this.ShapeFunction, this.random, 100); - } + this.random = rng; + this.fitter = new ShapeFitter(this.ShapeFunction, this.random, 100); } protected abstract double ShapeFunction(double x); - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + public async ValueTask GetDistributedValue(CancellationToken token = default) => await this.fitter.NextNumber(token); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/ExponentialLa10.cs b/FastRng/Double/Distributions/ExponentialLa10.cs index 47a4e46..eae90e4 100644 --- a/FastRng/Double/Distributions/ExponentialLa10.cs +++ b/FastRng/Double/Distributions/ExponentialLa10.cs @@ -9,6 +9,10 @@ namespace FastRng.Double.Distributions private const double LAMBDA = 10.0; private const double CONSTANT = 0.1106; + public ExponentialLa10(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(-LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/ExponentialLa5.cs b/FastRng/Double/Distributions/ExponentialLa5.cs index 9ccc806..dc59a19 100644 --- a/FastRng/Double/Distributions/ExponentialLa5.cs +++ b/FastRng/Double/Distributions/ExponentialLa5.cs @@ -9,6 +9,10 @@ namespace FastRng.Double.Distributions private const double LAMBDA = 5.0; private const double CONSTANT = 0.2103; + public ExponentialLa5(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(-LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/GammaA5B15.cs b/FastRng/Double/Distributions/GammaA5B15.cs index 6a9b455..7cd149c 100644 --- a/FastRng/Double/Distributions/GammaA5B15.cs +++ b/FastRng/Double/Distributions/GammaA5B15.cs @@ -18,6 +18,10 @@ namespace FastRng.Double.Distributions GAMMA_ALPHA = MathTools.Gamma(ALPHA); BETA_TO_THE_ALPHA = Math.Pow(BETA, ALPHA); } + + public GammaA5B15(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => CONSTANT * ((BETA_TO_THE_ALPHA * Math.Pow(x, ALPHA - 1.0d) * Math.Exp(-BETA * x)) / GAMMA_ALPHA); } diff --git a/FastRng/Double/Distributions/IDistribution.cs b/FastRng/Double/Distributions/IDistribution.cs index 779f12c..9211ac9 100644 --- a/FastRng/Double/Distributions/IDistribution.cs +++ b/FastRng/Double/Distributions/IDistribution.cs @@ -5,8 +5,6 @@ namespace FastRng.Double.Distributions { public interface IDistribution { - public IRandom Random { get; set; } - public ValueTask GetDistributedValue(CancellationToken token); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/InverseExponentialLa10.cs b/FastRng/Double/Distributions/InverseExponentialLa10.cs index 61d1397..edb1d97 100644 --- a/FastRng/Double/Distributions/InverseExponentialLa10.cs +++ b/FastRng/Double/Distributions/InverseExponentialLa10.cs @@ -9,6 +9,10 @@ namespace FastRng.Double.Distributions private const double LAMBDA = 10.0; private const double CONSTANT = 4.539992976248453e-06; + public InverseExponentialLa10(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/InverseExponentialLa5.cs b/FastRng/Double/Distributions/InverseExponentialLa5.cs index 696f2c8..d4e753a 100644 --- a/FastRng/Double/Distributions/InverseExponentialLa5.cs +++ b/FastRng/Double/Distributions/InverseExponentialLa5.cs @@ -9,6 +9,10 @@ namespace FastRng.Double.Distributions private const double LAMBDA = 5.0; private const double CONSTANT = 0.001347589399817; + public InverseExponentialLa5(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/InverseGammaA3B05.cs b/FastRng/Double/Distributions/InverseGammaA3B05.cs index ed5a273..1cf142a 100644 --- a/FastRng/Double/Distributions/InverseGammaA3B05.cs +++ b/FastRng/Double/Distributions/InverseGammaA3B05.cs @@ -19,6 +19,10 @@ namespace FastRng.Double.Distributions FACTOR_LEFT = CONSTANT * (betaToTheAlpha / gammaAlpha); } + + public InverseGammaA3B05(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => FACTOR_LEFT * Math.Pow(x, -ALPHA - 1.0d) * Math.Exp(-BETA / x); } diff --git a/FastRng/Double/Distributions/LaplaceB01M0.cs b/FastRng/Double/Distributions/LaplaceB01M0.cs index 9b54b32..3f1cdae 100644 --- a/FastRng/Double/Distributions/LaplaceB01M0.cs +++ b/FastRng/Double/Distributions/LaplaceB01M0.cs @@ -16,6 +16,10 @@ namespace FastRng.Double.Distributions { FACTOR_LEFT = CONSTANT / (2.0d * B); } + + public LaplaceB01M0(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => FACTOR_LEFT * Math.Exp(-Math.Abs(x - MU) / B); } diff --git a/FastRng/Double/Distributions/LaplaceB01M05.cs b/FastRng/Double/Distributions/LaplaceB01M05.cs index d050e7c..12c257e 100644 --- a/FastRng/Double/Distributions/LaplaceB01M05.cs +++ b/FastRng/Double/Distributions/LaplaceB01M05.cs @@ -16,6 +16,10 @@ namespace FastRng.Double.Distributions { FACTOR_LEFT = CONSTANT / (2.0d * B); } + + public LaplaceB01M05(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => FACTOR_LEFT * Math.Exp(-Math.Abs(x - MU) / B); } diff --git a/FastRng/Double/Distributions/LogNormalS1M0.cs b/FastRng/Double/Distributions/LogNormalS1M0.cs index 91e0b61..371f050 100644 --- a/FastRng/Double/Distributions/LogNormalS1M0.cs +++ b/FastRng/Double/Distributions/LogNormalS1M0.cs @@ -16,6 +16,10 @@ namespace FastRng.Double.Distributions { FACTOR = SIGMA * Math.Sqrt(2 * Math.PI); } + + public LogNormalS1M0(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => (CONSTANT / (x * FACTOR)) * Math.Exp( -(Math.Pow(Math.Log(x) - MU, 2) / (2 * Math.Pow(SIGMA, 2)))); } diff --git a/FastRng/Double/Distributions/NormalS02M05.cs b/FastRng/Double/Distributions/NormalS02M05.cs index c08067f..c6e7418 100644 --- a/FastRng/Double/Distributions/NormalS02M05.cs +++ b/FastRng/Double/Distributions/NormalS02M05.cs @@ -11,6 +11,10 @@ namespace FastRng.Double.Distributions private const double STDDEV = 0.2; private const double MEAN = 0.5; + public NormalS02M05(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => 1.0 / (STDDEV * SQRT_2_PI) * Math.Exp(-0.5 * Math.Pow((x - MEAN) / STDDEV, 2.0)); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/StudentTNu1.cs b/FastRng/Double/Distributions/StudentTNu1.cs index 5061b4e..d047406 100644 --- a/FastRng/Double/Distributions/StudentTNu1.cs +++ b/FastRng/Double/Distributions/StudentTNu1.cs @@ -21,6 +21,10 @@ namespace FastRng.Double.Distributions DIVISOR = Math.Sqrt(NU * Math.PI) * MathTools.Gamma(NU * 0.5d); EXPONENT = -((NU + 1.0d) * 0.5d); } + + public StudentTNu1(IRandom rng) : base(rng) + { + } protected override double ShapeFunction(double x) => CONSTANT * Math.Pow((DIVIDEND / DIVISOR) * Math.Pow(1.0d + Math.Pow(START + x * COMPRESS, 2) / NU, EXPONENT), COMPRESS); } diff --git a/FastRng/Double/Distributions/Uniform.cs b/FastRng/Double/Distributions/Uniform.cs index 49e92f7..7d8f7e9 100644 --- a/FastRng/Double/Distributions/Uniform.cs +++ b/FastRng/Double/Distributions/Uniform.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; @@ -5,8 +6,16 @@ namespace FastRng.Double.Distributions { public sealed class Uniform : IDistribution { - public IRandom Random { get; set; } + private readonly IRandom rng; - public async ValueTask GetDistributedValue(CancellationToken token = default) => this.Random == null ? double.NaN : await this.Random.GetUniform(token); + public Uniform(IRandom rng) + { + if (rng == null) + throw new ArgumentNullException(nameof(rng), "An IRandom implementation is needed."); + + this.rng = rng; + } + + public async ValueTask GetDistributedValue(CancellationToken token = default) => await this.rng.GetUniform(token); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/WeibullK05La1.cs b/FastRng/Double/Distributions/WeibullK05La1.cs index 520c3c9..5fc3191 100644 --- a/FastRng/Double/Distributions/WeibullK05La1.cs +++ b/FastRng/Double/Distributions/WeibullK05La1.cs @@ -10,6 +10,10 @@ namespace FastRng.Double.Distributions private const double LAMBDA = 1.0; private const double CONSTANT = 0.221034183615129; + public WeibullK05La1(IRandom rng) : base(rng) + { + } + protected override double ShapeFunction(double x) => CONSTANT * ( (K / LAMBDA) * Math.Pow(x / LAMBDA, K - 1.0d) * Math.Exp(-Math.Pow(x/LAMBDA, K))); } } \ No newline at end of file diff --git a/FastRng/Double/MultiThreadedRng.cs b/FastRng/Double/MultiThreadedRng.cs index 4d0b0b2..629eb23 100644 --- a/FastRng/Double/MultiThreadedRng.cs +++ b/FastRng/Double/MultiThreadedRng.cs @@ -309,8 +309,6 @@ namespace FastRng.Double } var range = rangeEnd - rangeStart; - distribution.Random ??= this; - var distributedValue = await distribution.GetDistributedValue(cancel); return (uint) ((distributedValue * range) + rangeStart); } @@ -325,8 +323,6 @@ namespace FastRng.Double } var range = rangeEnd - rangeStart; - distribution.Random = this; - var distributedValue = await distribution.GetDistributedValue(cancel); return (ulong) ((distributedValue * range) + rangeStart); } @@ -341,8 +337,6 @@ namespace FastRng.Double } var range = rangeEnd - rangeStart; - distribution.Random = this; - var distributedValue = await distribution.GetDistributedValue(cancel); return (distributedValue * range) + rangeStart; } diff --git a/FastRng/Double/ShapeFitter.cs b/FastRng/Double/ShapeFitter.cs index 172c7d1..d9b0350 100644 --- a/FastRng/Double/ShapeFitter.cs +++ b/FastRng/Double/ShapeFitter.cs @@ -15,11 +15,12 @@ namespace FastRng.Double private readonly IRandom rng; private readonly double max; private readonly double sampleSize; - private readonly IDistribution uniform = new Uniform(); + private readonly IDistribution uniform; public ShapeFitter(Func shapeFunction, IRandom rng, ushort sampleSize = 50) { this.rng = rng; + this.uniform = new Uniform(rng); this.sampleSize = sampleSize; this.probabilities = new double[sampleSize]; diff --git a/FastRng/Float/Distributions/BetaA2B2.cs b/FastRng/Float/Distributions/BetaA2B2.cs index d533d96..98f5db4 100644 --- a/FastRng/Float/Distributions/BetaA2B2.cs +++ b/FastRng/Float/Distributions/BetaA2B2.cs @@ -8,6 +8,10 @@ namespace FastRng.Float.Distributions private const float BETA = 2f; private const float CONSTANT = 4f; + public BetaA2B2(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * MathF.Pow(x, ALPHA - 1f) * MathF.Pow(1f - x, BETA - 1f); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/BetaA2B5.cs b/FastRng/Float/Distributions/BetaA2B5.cs index d3ea994..7b6e620 100644 --- a/FastRng/Float/Distributions/BetaA2B5.cs +++ b/FastRng/Float/Distributions/BetaA2B5.cs @@ -8,6 +8,10 @@ namespace FastRng.Float.Distributions private const float BETA = 5f; private const float CONSTANT = 12.2f; + public BetaA2B5(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * MathF.Pow(x, ALPHA - 1f) * MathF.Pow(1f - x, BETA - 1f); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/BetaA5B2.cs b/FastRng/Float/Distributions/BetaA5B2.cs index 79fbc45..50bdc12 100644 --- a/FastRng/Float/Distributions/BetaA5B2.cs +++ b/FastRng/Float/Distributions/BetaA5B2.cs @@ -8,6 +8,10 @@ namespace FastRng.Float.Distributions private const float BETA = 2f; private const float CONSTANT = 12.2f; + public BetaA5B2(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * MathF.Pow(x, ALPHA - 1f) * MathF.Pow(1f - x, BETA - 1f); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/CauchyLorentzX0.cs b/FastRng/Float/Distributions/CauchyLorentzX0.cs index 3875d1d..814738a 100644 --- a/FastRng/Float/Distributions/CauchyLorentzX0.cs +++ b/FastRng/Float/Distributions/CauchyLorentzX0.cs @@ -8,6 +8,10 @@ namespace FastRng.Float.Distributions private const float SCALE = 0.1f; private const float MEDIAN = 0.0f; + public CauchyLorentzX0(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * (1.0f / (MathF.PI * SCALE)) * ((SCALE * SCALE) / (MathF.Pow(x - MEDIAN, 2f) + (SCALE * SCALE))); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/CauchyLorentzX1.cs b/FastRng/Float/Distributions/CauchyLorentzX1.cs index 633001e..7fdf4f7 100644 --- a/FastRng/Float/Distributions/CauchyLorentzX1.cs +++ b/FastRng/Float/Distributions/CauchyLorentzX1.cs @@ -8,6 +8,10 @@ namespace FastRng.Float.Distributions private const float SCALE = 0.1f; private const float MEDIAN = 1.0f; + public CauchyLorentzX1(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * (1.0f / (MathF.PI * SCALE)) * ((SCALE * SCALE) / (MathF.Pow(x - MEDIAN, 2f) + (SCALE * SCALE))); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/ChiSquareK1.cs b/FastRng/Float/Distributions/ChiSquareK1.cs index c8c3fd5..623541c 100644 --- a/FastRng/Float/Distributions/ChiSquareK1.cs +++ b/FastRng/Float/Distributions/ChiSquareK1.cs @@ -17,6 +17,10 @@ namespace FastRng.Float.Distributions var gammaKHalf = MathTools.Gamma(K_HALF); DIVISOR = twoToTheKHalf * gammaKHalf; } + + public ChiSquareK1(IRandom rng) : base(rng) + { + } protected override float ShapeFunction(float x) => CONSTANT * ((MathF.Pow(x, K_HALF_MINUS_ONE) * MathF.Exp(-x * 0.5f)) / DIVISOR); } diff --git a/FastRng/Float/Distributions/ChiSquareK10.cs b/FastRng/Float/Distributions/ChiSquareK10.cs index 8dbdd6e..fda0510 100644 --- a/FastRng/Float/Distributions/ChiSquareK10.cs +++ b/FastRng/Float/Distributions/ChiSquareK10.cs @@ -17,6 +17,10 @@ namespace FastRng.Float.Distributions var gammaKHalf = MathTools.Gamma(K_HALF); DIVISOR = twoToTheKHalf * gammaKHalf; } + + public ChiSquareK10(IRandom rng) : base(rng) + { + } protected override float ShapeFunction(float x) => CONSTANT * ((MathF.Pow(x, K_HALF_MINUS_ONE) * MathF.Exp(-x * 0.5f)) / DIVISOR); } diff --git a/FastRng/Float/Distributions/ChiSquareK4.cs b/FastRng/Float/Distributions/ChiSquareK4.cs index aa545f4..64d48b8 100644 --- a/FastRng/Float/Distributions/ChiSquareK4.cs +++ b/FastRng/Float/Distributions/ChiSquareK4.cs @@ -18,6 +18,10 @@ namespace FastRng.Float.Distributions DIVISOR = twoToTheKHalf * gammaKHalf; } + public ChiSquareK4(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * ((MathF.Pow(x, K_HALF_MINUS_ONE) * MathF.Exp(-x * 0.5f)) / DIVISOR); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/Distribution.cs b/FastRng/Float/Distributions/Distribution.cs index 47a86ce..2780fc9 100644 --- a/FastRng/Float/Distributions/Distribution.cs +++ b/FastRng/Float/Distributions/Distribution.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; @@ -5,30 +6,20 @@ namespace FastRng.Float.Distributions { public abstract class Distribution : IDistribution { - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random + private readonly ShapeFitter fitter; + private readonly IRandom random; + + protected Distribution(IRandom rng) { - get => this.random; - set - { - if(this.random != null) - return; + if (rng == null) + throw new ArgumentNullException(nameof(rng), "An IRandom implementation is needed."); - this.random = value; - this.fitter = new ShapeFitter(this.ShapeFunction, this.random, 100); - } + this.random = rng; + this.fitter = new ShapeFitter(this.ShapeFunction, this.random, 100); } protected abstract float ShapeFunction(float x); - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return float.NaN; - - return await this.fitter.NextNumber(token); - } + public async ValueTask GetDistributedValue(CancellationToken token = default) => await this.fitter.NextNumber(token); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/ExponentialLa10.cs b/FastRng/Float/Distributions/ExponentialLa10.cs index e2f83c2..bc2708b 100644 --- a/FastRng/Float/Distributions/ExponentialLa10.cs +++ b/FastRng/Float/Distributions/ExponentialLa10.cs @@ -7,6 +7,10 @@ namespace FastRng.Float.Distributions private const float LAMBDA = 10.0f; private const float CONSTANT = 0.1106f; + public ExponentialLa10(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * LAMBDA * MathF.Exp(-LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/ExponentialLa5.cs b/FastRng/Float/Distributions/ExponentialLa5.cs index 3575aed..dac3f68 100644 --- a/FastRng/Float/Distributions/ExponentialLa5.cs +++ b/FastRng/Float/Distributions/ExponentialLa5.cs @@ -7,6 +7,10 @@ namespace FastRng.Float.Distributions private const float LAMBDA = 5.0f; private const float CONSTANT = 0.2103f; + public ExponentialLa5(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * LAMBDA * MathF.Exp(-LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/GammaA5B15.cs b/FastRng/Float/Distributions/GammaA5B15.cs index 86d55f6..7dfd20f 100644 --- a/FastRng/Float/Distributions/GammaA5B15.cs +++ b/FastRng/Float/Distributions/GammaA5B15.cs @@ -17,6 +17,10 @@ namespace FastRng.Float.Distributions BETA_TO_THE_ALPHA = MathF.Pow(BETA, ALPHA); } + public GammaA5B15(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * ((BETA_TO_THE_ALPHA * MathF.Pow(x, ALPHA - 1.0f) * MathF.Exp(-BETA * x)) / GAMMA_ALPHA); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/IDistribution.cs b/FastRng/Float/Distributions/IDistribution.cs index 9b80c08..0e1a2d5 100644 --- a/FastRng/Float/Distributions/IDistribution.cs +++ b/FastRng/Float/Distributions/IDistribution.cs @@ -5,8 +5,6 @@ namespace FastRng.Float.Distributions { public interface IDistribution { - public IRandom Random { get; set; } - public ValueTask GetDistributedValue(CancellationToken token); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/InverseExponentialLa10.cs b/FastRng/Float/Distributions/InverseExponentialLa10.cs index 936cf51..3efd4fd 100644 --- a/FastRng/Float/Distributions/InverseExponentialLa10.cs +++ b/FastRng/Float/Distributions/InverseExponentialLa10.cs @@ -7,6 +7,10 @@ namespace FastRng.Float.Distributions private const float LAMBDA = 10.0f; private const float CONSTANT = 4.539992976248453e-06f; + public InverseExponentialLa10(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * LAMBDA * MathF.Exp(LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/InverseExponentialLa5.cs b/FastRng/Float/Distributions/InverseExponentialLa5.cs index 7705d62..f8bccaa 100644 --- a/FastRng/Float/Distributions/InverseExponentialLa5.cs +++ b/FastRng/Float/Distributions/InverseExponentialLa5.cs @@ -7,6 +7,10 @@ namespace FastRng.Float.Distributions private const float LAMBDA = 5.0f; private const float CONSTANT = 0.001347589399817f; + public InverseExponentialLa5(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * LAMBDA * MathF.Exp(LAMBDA * x); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/InverseGammaA3B05.cs b/FastRng/Float/Distributions/InverseGammaA3B05.cs index b9a7678..c18434f 100644 --- a/FastRng/Float/Distributions/InverseGammaA3B05.cs +++ b/FastRng/Float/Distributions/InverseGammaA3B05.cs @@ -17,6 +17,10 @@ namespace FastRng.Float.Distributions FACTOR_LEFT = CONSTANT * (betaToTheAlpha / gammaAlpha); } + + public InverseGammaA3B05(IRandom rng) : base(rng) + { + } protected override float ShapeFunction(float x) => FACTOR_LEFT * MathF.Pow(x, -ALPHA - 1.0f) * MathF.Exp(-BETA / x); } diff --git a/FastRng/Float/Distributions/LaplaceB01M0.cs b/FastRng/Float/Distributions/LaplaceB01M0.cs index 6bdfc90..25074d0 100644 --- a/FastRng/Float/Distributions/LaplaceB01M0.cs +++ b/FastRng/Float/Distributions/LaplaceB01M0.cs @@ -14,6 +14,10 @@ namespace FastRng.Float.Distributions { FACTOR_LEFT = CONSTANT / (2.0f * B); } + + public LaplaceB01M0(IRandom rng) : base(rng) + { + } protected override float ShapeFunction(float x) => FACTOR_LEFT * MathF.Exp(-MathF.Abs(x - MU) / B); } diff --git a/FastRng/Float/Distributions/LaplaceB01M05.cs b/FastRng/Float/Distributions/LaplaceB01M05.cs index 3bb4710..4cc1eb6 100644 --- a/FastRng/Float/Distributions/LaplaceB01M05.cs +++ b/FastRng/Float/Distributions/LaplaceB01M05.cs @@ -14,6 +14,10 @@ namespace FastRng.Float.Distributions { FACTOR_LEFT = CONSTANT / (2.0f * B); } + + public LaplaceB01M05(IRandom rng) : base(rng) + { + } protected override float ShapeFunction(float x) => FACTOR_LEFT * MathF.Exp(-MathF.Abs(x - MU) / B); } diff --git a/FastRng/Float/Distributions/LogNormalS1M0.cs b/FastRng/Float/Distributions/LogNormalS1M0.cs index 7ddfb10..dd7ed02 100644 --- a/FastRng/Float/Distributions/LogNormalS1M0.cs +++ b/FastRng/Float/Distributions/LogNormalS1M0.cs @@ -14,6 +14,10 @@ namespace FastRng.Float.Distributions { FACTOR = SIGMA * MathF.Sqrt(2f * MathF.PI); } + + public LogNormalS1M0(IRandom rng) : base(rng) + { + } protected override float ShapeFunction(float x) => (CONSTANT / (x * FACTOR)) * MathF.Exp( -(MathF.Pow(MathF.Log(x) - MU, 2f) / (2f * MathF.Pow(SIGMA, 2f)))); } diff --git a/FastRng/Float/Distributions/NormalS02M05.cs b/FastRng/Float/Distributions/NormalS02M05.cs index 7e3cac1..fa3d3a9 100644 --- a/FastRng/Float/Distributions/NormalS02M05.cs +++ b/FastRng/Float/Distributions/NormalS02M05.cs @@ -8,6 +8,10 @@ namespace FastRng.Float.Distributions private const float STDDEV = 0.2f; private const float MEAN = 0.5f; + public NormalS02M05(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => 1.0f / (STDDEV * SQRT_2_PI) * MathF.Exp(-0.5f * MathF.Pow((x - MEAN) / STDDEV, 2.0f)); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/StudentTNu1.cs b/FastRng/Float/Distributions/StudentTNu1.cs index 8fb0910..10ddc67 100644 --- a/FastRng/Float/Distributions/StudentTNu1.cs +++ b/FastRng/Float/Distributions/StudentTNu1.cs @@ -19,6 +19,10 @@ namespace FastRng.Float.Distributions DIVISOR = MathF.Sqrt(NU * MathF.PI) * MathTools.Gamma(NU * 0.5f); EXPONENT = -((NU + 1.0f) * 0.5f); } + + public StudentTNu1(IRandom rng) : base(rng) + { + } protected override float ShapeFunction(float x) => CONSTANT * MathF.Pow((DIVIDEND / DIVISOR) * MathF.Pow(1.0f + MathF.Pow(START + x * COMPRESS, 2f) / NU, EXPONENT), COMPRESS); } diff --git a/FastRng/Float/Distributions/Uniform.cs b/FastRng/Float/Distributions/Uniform.cs index fc593a2..bd11774 100644 --- a/FastRng/Float/Distributions/Uniform.cs +++ b/FastRng/Float/Distributions/Uniform.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; @@ -5,8 +6,16 @@ namespace FastRng.Float.Distributions { public sealed class Uniform : IDistribution { - public IRandom Random { get; set; } + private readonly IRandom rng; - public async ValueTask GetDistributedValue(CancellationToken token = default) => this.Random == null ? float.NaN : await this.Random.GetUniform(token); + public Uniform(IRandom rng) + { + if (rng == null) + throw new ArgumentNullException(nameof(rng), "An IRandom implementation is needed."); + + this.rng = rng; + } + + public async ValueTask GetDistributedValue(CancellationToken token = default) => await this.rng.GetUniform(token); } } \ No newline at end of file diff --git a/FastRng/Float/Distributions/WeibullK05La1.cs b/FastRng/Float/Distributions/WeibullK05La1.cs index 518447f..9415dad 100644 --- a/FastRng/Float/Distributions/WeibullK05La1.cs +++ b/FastRng/Float/Distributions/WeibullK05La1.cs @@ -8,6 +8,10 @@ namespace FastRng.Float.Distributions private const float LAMBDA = 1.0f; private const float CONSTANT = 0.221034183615129f; + public WeibullK05La1(IRandom rng) : base(rng) + { + } + protected override float ShapeFunction(float x) => CONSTANT * ( (K / LAMBDA) * MathF.Pow(x / LAMBDA, K - 1.0f) * MathF.Exp(-MathF.Pow(x/LAMBDA, K))); } } \ No newline at end of file diff --git a/FastRng/Float/MultiThreadedRng.cs b/FastRng/Float/MultiThreadedRng.cs index f8fb392..2456479 100644 --- a/FastRng/Float/MultiThreadedRng.cs +++ b/FastRng/Float/MultiThreadedRng.cs @@ -309,8 +309,6 @@ namespace FastRng.Float } var range = rangeEnd - rangeStart; - distribution.Random ??= this; - var distributedValue = await distribution.GetDistributedValue(cancel); return (uint) ((distributedValue * range) + rangeStart); } @@ -325,8 +323,6 @@ namespace FastRng.Float } var range = rangeEnd - rangeStart; - distribution.Random = this; - var distributedValue = await distribution.GetDistributedValue(cancel); return (ulong) ((distributedValue * range) + rangeStart); } @@ -341,8 +337,6 @@ namespace FastRng.Float } var range = rangeEnd - rangeStart; - distribution.Random = this; - var distributedValue = await distribution.GetDistributedValue(cancel); return (distributedValue * range) + rangeStart; } diff --git a/FastRng/Float/ShapeFitter.cs b/FastRng/Float/ShapeFitter.cs index 899fd25..2f9a05e 100644 --- a/FastRng/Float/ShapeFitter.cs +++ b/FastRng/Float/ShapeFitter.cs @@ -14,11 +14,12 @@ namespace FastRng.Float private readonly IRandom rng; private readonly float max; private readonly float sampleSize; - private readonly IDistribution uniform = new Uniform(); + private readonly IDistribution uniform; public ShapeFitter(Func shapeFunction, IRandom rng, ushort sampleSize = 50) { this.rng = rng; + this.uniform = new Uniform(rng); this.sampleSize = sampleSize; this.probabilities = new float[sampleSize]; diff --git a/FastRngTests/Double/Distributions/BetaA2B2.cs b/FastRngTests/Double/Distributions/BetaA2B2.cs index 4c7a1b2..91c5b9b 100644 --- a/FastRngTests/Double/Distributions/BetaA2B2.cs +++ b/FastRngTests/Double/Distributions/BetaA2B2.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaDistribution01() { - var dist = new FastRng.Double.Distributions.BetaA2B2(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.BetaA2B2(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0396).Within(0.3)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.BetaA2B2(); + var dist = new FastRng.Double.Distributions.BetaA2B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.BetaA2B2(); + var dist = new FastRng.Double.Distributions.BetaA2B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestBetaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.BetaA2B2 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.BetaA2B2(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.BetaA2B2(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/BetaA2B5.cs b/FastRngTests/Double/Distributions/BetaA2B5.cs index ca95f82..8abb944 100644 --- a/FastRngTests/Double/Distributions/BetaA2B5.cs +++ b/FastRngTests/Double/Distributions/BetaA2B5.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaDistribution01() { - var dist = new FastRng.Double.Distributions.BetaA2B5(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.BetaA2B5(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.11719271).Within(0.3)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.BetaA2B5(); + var dist = new FastRng.Double.Distributions.BetaA2B5(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.BetaA2B5(); + var dist = new FastRng.Double.Distributions.BetaA2B5(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestBetaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.BetaA2B5 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.BetaA2B5(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.BetaA2B5(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/BetaA5B2.cs b/FastRngTests/Double/Distributions/BetaA5B2.cs index 2dc242b..264cfc9 100644 --- a/FastRngTests/Double/Distributions/BetaA5B2.cs +++ b/FastRngTests/Double/Distributions/BetaA5B2.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaDistribution01() { - var dist = new FastRng.Double.Distributions.BetaA5B2(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.BetaA5B2(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000001).Within(0.0000003)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.BetaA5B2(); + var dist = new FastRng.Double.Distributions.BetaA5B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.BetaA5B2(); + var dist = new FastRng.Double.Distributions.BetaA5B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestBetaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.BetaA5B2 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.BetaA5B2(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.BetaA5B2(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/CauchyLorentzX0.cs b/FastRngTests/Double/Distributions/CauchyLorentzX0.cs index b31ce6c..67cd020 100644 --- a/FastRngTests/Double/Distributions/CauchyLorentzX0.cs +++ b/FastRngTests/Double/Distributions/CauchyLorentzX0.cs @@ -17,15 +17,14 @@ namespace FastRngTests.Double.Distributions { // The properties of the cauchy distribution cannot be tested by mean, media or variance, // cf. https://en.wikipedia.org/wiki/Cauchy_distribution#Explanation_of_undefined_moments - - var dist = new FastRng.Double.Distributions.CauchyLorentzX0(); + + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.CauchyLorentzX0(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.976990739772031).Within(0.06)); @@ -52,13 +51,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.CauchyLorentzX0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -68,30 +66,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.CauchyLorentzX0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestCauchyGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.CauchyLorentzX0 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -99,11 +79,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.CauchyLorentzX0(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.CauchyLorentzX0(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/CauchyLorentzX1.cs b/FastRngTests/Double/Distributions/CauchyLorentzX1.cs index ed559fc..fcda813 100644 --- a/FastRngTests/Double/Distributions/CauchyLorentzX1.cs +++ b/FastRngTests/Double/Distributions/CauchyLorentzX1.cs @@ -17,15 +17,14 @@ namespace FastRngTests.Double.Distributions { // The properties of the cauchy distribution cannot be tested by mean, media or variance, // cf. https://en.wikipedia.org/wiki/Cauchy_distribution#Explanation_of_undefined_moments - - var dist = new FastRng.Double.Distributions.CauchyLorentzX1(); + + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.CauchyLorentzX1(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.009966272570142).Within(0.003)); @@ -52,13 +51,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.CauchyLorentzX0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -68,30 +66,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.CauchyLorentzX0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestCauchyGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.CauchyLorentzX0 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -99,11 +79,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.CauchyLorentzX1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.CauchyLorentzX1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/ChiSquareK1.cs b/FastRngTests/Double/Distributions/ChiSquareK1.cs index 7bd9845..7a44cfc 100644 --- a/FastRngTests/Double/Distributions/ChiSquareK1.cs +++ b/FastRngTests/Double/Distributions/ChiSquareK1.cs @@ -15,17 +15,15 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareDistribution01() { - var dist = new FastRng.Double.Distributions.ChiSquareK1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK1(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) { var value = await rng.NextNumber(dist); fqa.CountThis(value); } - - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); @@ -53,13 +51,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.ChiSquareK1(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK1(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -69,13 +66,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.ChiSquareK1(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK1(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -83,28 +79,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestChiSquareGeneratorWithRange03() + public void NoRandomNumberGenerator01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.ChiSquareK1 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() - { - var dist = new FastRng.Double.Distributions.ChiSquareK1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.ChiSquareK1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/ChiSquareK10.cs b/FastRngTests/Double/Distributions/ChiSquareK10.cs index b8f5f6a..6067b35 100644 --- a/FastRngTests/Double/Distributions/ChiSquareK10.cs +++ b/FastRngTests/Double/Distributions/ChiSquareK10.cs @@ -15,17 +15,15 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareDistribution01() { - var dist = new FastRng.Double.Distributions.ChiSquareK10(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK10(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) { var value = await rng.NextNumber(dist); fqa.CountThis(value); } - - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); @@ -53,13 +51,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.ChiSquareK10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK10(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -69,30 +66,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.ChiSquareK10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK10(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestChiSquareGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.ChiSquareK10 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -100,11 +79,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.ChiSquareK10(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.ChiSquareK10(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/ChiSquareK4.cs b/FastRngTests/Double/Distributions/ChiSquareK4.cs index c84e7e7..02e18a0 100644 --- a/FastRngTests/Double/Distributions/ChiSquareK4.cs +++ b/FastRngTests/Double/Distributions/ChiSquareK4.cs @@ -15,14 +15,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareDistribution01() { - var dist = new FastRng.Double.Distributions.ChiSquareK4(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK4(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); @@ -50,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.ChiSquareK4(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK4(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -66,13 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.ChiSquareK4(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ChiSquareK4(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -80,28 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestChiSquareGeneratorWithRange03() + public void NoRandomNumberGenerator01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.ChiSquareK4 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() - { - var dist = new FastRng.Double.Distributions.ChiSquareK4(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.ChiSquareK4(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/ExponentialLa10.cs b/FastRngTests/Double/Distributions/ExponentialLa10.cs index d0f43f1..3e5e876 100644 --- a/FastRngTests/Double/Distributions/ExponentialLa10.cs +++ b/FastRngTests/Double/Distributions/ExponentialLa10.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Double.Distributions.ExponentialLa10(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ExponentialLa10(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.00075018434777).Within(0.05)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.ExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ExponentialLa10(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.ExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ExponentialLa10(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.ExponentialLa10 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.ExponentialLa10(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.ExponentialLa10(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/ExponentialLa5.cs b/FastRngTests/Double/Distributions/ExponentialLa5.cs index f32fc39..bb73bcf 100644 --- a/FastRngTests/Double/Distributions/ExponentialLa5.cs +++ b/FastRngTests/Double/Distributions/ExponentialLa5.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Double.Distributions.ExponentialLa5(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ExponentialLa5(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.0002177398625).Within(0.05)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.ExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ExponentialLa5(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.ExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.ExponentialLa5(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.ExponentialLa5 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.ExponentialLa5(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.ExponentialLa5(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/GammaA5B15.cs b/FastRngTests/Double/Distributions/GammaA5B15.cs index 6c6c89d..dc0fb53 100644 --- a/FastRngTests/Double/Distributions/GammaA5B15.cs +++ b/FastRngTests/Double/Distributions/GammaA5B15.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestGammaDistribution01() { - var dist = new FastRng.Double.Distributions.GammaA5B15(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.GammaA5B15(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000929594237282).Within(0.0008)); @@ -44,35 +43,17 @@ namespace FastRngTests.Double.Distributions Assert.That(result[99], Is.EqualTo(0.0033038503429554).Within(0.008)); } - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestGammaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.GammaA5B15 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] public async Task TestGammaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.GammaA5B15(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.GammaA5B15(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -82,13 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestGammaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.GammaA5B15(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.GammaA5B15(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.GammaA5B15(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.GammaA5B15(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/InverseExponentialLa10.cs b/FastRngTests/Double/Distributions/InverseExponentialLa10.cs index 897de98..4d31cd0 100644 --- a/FastRngTests/Double/Distributions/InverseExponentialLa10.cs +++ b/FastRngTests/Double/Distributions/InverseExponentialLa10.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa10(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseExponentialLa10(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000501746820562).Within(0.0003)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseExponentialLa10(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseExponentialLa10(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.InverseExponentialLa10 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa10(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.InverseExponentialLa10(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/InverseExponentialLa5.cs b/FastRngTests/Double/Distributions/InverseExponentialLa5.cs index c894f9d..93c6c7d 100644 --- a/FastRngTests/Double/Distributions/InverseExponentialLa5.cs +++ b/FastRngTests/Double/Distributions/InverseExponentialLa5.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa5(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseExponentialLa5(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.007083408929052).Within(0.008)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseExponentialLa5(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseExponentialLa5(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.InverseExponentialLa5 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.InverseExponentialLa5(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.InverseExponentialLa5(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/InverseGammaA3B05.cs b/FastRngTests/Double/Distributions/InverseGammaA3B05.cs index 886c7d9..db120a1 100644 --- a/FastRngTests/Double/Distributions/InverseGammaA3B05.cs +++ b/FastRngTests/Double/Distributions/InverseGammaA3B05.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestInverseGammaDistribution01() { - var dist = new FastRng.Double.Distributions.InverseGammaA3B05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseGammaA3B05(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000000000000003).Within(0.0000001)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestInverseGammaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.InverseGammaA3B05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseGammaA3B05(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,13 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestInverseGammaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.InverseGammaA3B05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.InverseGammaA3B05(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -79,28 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestInverseGammaGeneratorWithRange03() + public void NoRandomNumberGenerator01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.InverseGammaA3B05 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() - { - var dist = new FastRng.Double.Distributions.InverseGammaA3B05(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.InverseGammaA3B05(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/LaplaceB01M0.cs b/FastRngTests/Double/Distributions/LaplaceB01M0.cs index 4589ba9..b9ae2da 100644 --- a/FastRngTests/Double/Distributions/LaplaceB01M0.cs +++ b/FastRngTests/Double/Distributions/LaplaceB01M0.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceDistribution01() { - var dist = new FastRng.Double.Distributions.LaplaceB01M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LaplaceB01M0(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.0000000000000000).Within(0.05)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LaplaceB01M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LaplaceB01M0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LaplaceB01M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LaplaceB01M0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestLaplaceGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LaplaceB01M0 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.LaplaceB01M0(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.LaplaceB01M0(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/LaplaceB01M05.cs b/FastRngTests/Double/Distributions/LaplaceB01M05.cs index 56f5dd2..65d1448 100644 --- a/FastRngTests/Double/Distributions/LaplaceB01M05.cs +++ b/FastRngTests/Double/Distributions/LaplaceB01M05.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceDistribution01() { - var dist = new FastRng.Double.Distributions.LaplaceB01M05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LaplaceB01M05(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0074465830709244).Within(0.004)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LaplaceB01M05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LaplaceB01M05(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LaplaceB01M05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LaplaceB01M05(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestLaplaceGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LaplaceB01M05 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.LaplaceB01M05(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.LaplaceB01M05(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/LogNormalS1M0.cs b/FastRngTests/Double/Distributions/LogNormalS1M0.cs index 5b56566..6518208 100644 --- a/FastRngTests/Double/Distributions/LogNormalS1M0.cs +++ b/FastRngTests/Double/Distributions/LogNormalS1M0.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLogNormalDistribution01() { - var dist = new FastRng.Double.Distributions.LogNormalS1M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LogNormalS1M0(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.001505531).Within(0.003)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLogNormalGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LogNormalS1M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LogNormalS1M0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestLogNormalGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LogNormalS1M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.LogNormalS1M0(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestLogNormalGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.LogNormalS1M0 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.LogNormalS1M0(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.LogNormalS1M0(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/NormalS02M05.cs b/FastRngTests/Double/Distributions/NormalS02M05.cs index e5489c2..5a711ba 100644 --- a/FastRngTests/Double/Distributions/NormalS02M05.cs +++ b/FastRngTests/Double/Distributions/NormalS02M05.cs @@ -18,10 +18,10 @@ namespace FastRngTests.Double.Distributions const double MEAN = 0.5; const double STANDARD_DEVIATION = 0.2; - var dist = new FastRng.Double.Distributions.NormalS02M05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.NormalS02M05(rng); var stats = new RunningStatistics(); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) { @@ -30,7 +30,6 @@ namespace FastRngTests.Double.Distributions fra.CountThis(nextNumber); } - rng.StopProducer(); fra.NormalizeAndPlotEvents(TestContext.WriteLine); TestContext.WriteLine($"mean={MEAN} vs. {stats.Mean}"); @@ -45,13 +44,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestNormalGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.NormalS02M05(); + var dist = new FastRng.Double.Distributions.NormalS02M05(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -61,30 +59,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestNormalGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.NormalS02M05(); + var dist = new FastRng.Double.Distributions.NormalS02M05(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestNormalGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.NormalS02M05 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -92,11 +72,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.NormalS02M05(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.NormalS02M05(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/StudentTNu1.cs b/FastRngTests/Double/Distributions/StudentTNu1.cs index f56b9f1..5f2a59d 100644 --- a/FastRngTests/Double/Distributions/StudentTNu1.cs +++ b/FastRngTests/Double/Distributions/StudentTNu1.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestStudentTDistribution01() { - var dist = new FastRng.Double.Distributions.StudentTNu1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.StudentTNu1(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.000000000).Within(0.2)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestStudentTGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.StudentTNu1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.StudentTNu1(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestStudentTGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.StudentTNu1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.StudentTNu1(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestStudentTGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.StudentTNu1 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.StudentTNu1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.StudentTNu1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/Uniform.cs b/FastRngTests/Double/Distributions/Uniform.cs index 025952d..82ad284 100644 --- a/FastRngTests/Double/Distributions/Uniform.cs +++ b/FastRngTests/Double/Distributions/Uniform.cs @@ -10,8 +10,6 @@ namespace FastRngTests.Double.Distributions [ExcludeFromCodeCoverage] public class Uniform { - private readonly IRandom rng = new MultiThreadedRng(); - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] @@ -24,7 +22,7 @@ namespace FastRngTests.Double.Distributions var stats = new RunningStatistics(); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) { @@ -33,7 +31,6 @@ namespace FastRngTests.Double.Distributions fra.CountThis(value); } - rng.StopProducer(); fra.NormalizeAndPlotEvents(TestContext.WriteLine); fra.PlotOccurence(TestContext.WriteLine); TestContext.WriteLine($"mean={MEAN} vs. {stats.Mean}"); @@ -61,13 +58,12 @@ namespace FastRngTests.Double.Distributions const double P_HIGH = 1.0 - 0.25 * FAILURE_PROBABILITY; var samples = new double[NUM_ROUNDS]; - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); int n; for (n = 0; n != NUM_ROUNDS; ++n) samples[n] = await rng.GetUniform(); - rng.StopProducer(); Array.Sort(samples); var jMinus = 0; @@ -120,11 +116,11 @@ namespace FastRngTests.Double.Distributions public async Task TestUniformGeneratorWithRange01() { var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -135,42 +131,25 @@ namespace FastRngTests.Double.Distributions public async Task TestUniformGeneratorWithRange02() { var samples = new double[1_000]; - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestUniformGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.Uniform { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - + [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] public async Task TestUniformGeneratorWithRange04() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.GetUniform(); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -180,7 +159,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestRange05Uint() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -195,7 +175,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestRange05Ulong() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -210,7 +191,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestRange05Float() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -224,7 +206,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestDistribution001Uint() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -237,7 +220,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestDistribution001Ulong() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -250,7 +234,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestDistribution001Float() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -263,7 +248,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.LONG_RUNNING)] public async Task TestDistribution002Uint() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 100_000_000; for (var n = 0; n < runs; n++) @@ -276,7 +262,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.LONG_RUNNING)] public async Task TestDistribution002Ulong() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 100_000_000; for (var n = 0; n < runs; n++) @@ -289,7 +276,8 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.LONG_RUNNING)] public async Task TestDistribution002Float() { - var dist = new FastRng.Double.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 100_000_000; for (var n = 0; n < runs; n++) diff --git a/FastRngTests/Double/Distributions/WeibullK05La1.cs b/FastRngTests/Double/Distributions/WeibullK05La1.cs index 6faaa55..5345eec 100644 --- a/FastRngTests/Double/Distributions/WeibullK05La1.cs +++ b/FastRngTests/Double/Distributions/WeibullK05La1.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestWeibullDistribution01() { - var dist = new FastRng.Double.Distributions.WeibullK05La1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.WeibullK05La1(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.000000000).Within(0.2)); @@ -49,13 +48,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestWeibullGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.WeibullK05La1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.WeibullK05La1(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0, 1.0, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Double.Distributions [Category(TestCategories.NORMAL)] public async Task TestWeibullGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.WeibullK05La1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Double.Distributions.WeibullK05La1(rng); var samples = new double[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0, 1.0, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestWeibullGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Double.Distributions.WeibullK05La1 { Random = rng }; // Test default parameters - - var samples = new double[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Double.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Double.Distributions.WeibullK05La1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Double.Distributions.WeibullK05La1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Double/MultiThreadedRngTests.cs b/FastRngTests/Double/MultiThreadedRngTests.cs index a0f54f2..17a2691 100644 --- a/FastRngTests/Double/MultiThreadedRngTests.cs +++ b/FastRngTests/Double/MultiThreadedRngTests.cs @@ -18,7 +18,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange01Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); for (uint n = 0; n < 1_000_000; n++) Assert.That(await rng.NextNumber(n, 100_000 + n, dist), Is.InRange(n, 100_000 + n)); } @@ -28,7 +28,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange01Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); for (ulong n = 0; n < 1_000_000; n++) Assert.That(await rng.NextNumber(n, 100_000 + n, dist), Is.InRange(n, 100_000 + n)); } @@ -38,7 +38,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange01Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); for (var n = 0.0; n < 1e6; n++) Assert.That(await rng.NextNumber(n, 100_000 + n, dist), Is.InRange(n, 100_000 + n)); } @@ -48,7 +48,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange02Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5, 5, dist), Is.EqualTo(5)); Assert.That(await rng.NextNumber(0, 0, dist), Is.EqualTo(0)); Assert.That(await rng.NextNumber(3_000_000_000, 3_000_000_000, dist), Is.EqualTo(3_000_000_000)); @@ -59,7 +59,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange02Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5UL, 5, dist), Is.EqualTo(5)); Assert.That(await rng.NextNumber(0UL, 0, dist), Is.EqualTo(0)); Assert.That(await rng.NextNumber(3_000_000_000UL, 3_000_000_000, dist), Is.EqualTo(3_000_000_000)); @@ -70,7 +70,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange02Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5f, 5f, dist), Is.EqualTo(5)); Assert.That(await rng.NextNumber(0f, 0f, dist), Is.EqualTo(0)); Assert.That(await rng.NextNumber(3e9, 3e9, dist), Is.EqualTo(3e9)); @@ -81,7 +81,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange03Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5, 6, dist), Is.InRange(5, 6)); Assert.That(await rng.NextNumber(0, 1, dist), Is.InRange(0, 1)); Assert.That(await rng.NextNumber(3_000_000_000, 3_000_000_002, dist), Is.InRange(3_000_000_000, 3_000_000_002)); @@ -92,7 +92,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange03Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5UL, 6, dist), Is.InRange(5, 6)); Assert.That(await rng.NextNumber(0UL, 1, dist), Is.InRange(0, 1)); Assert.That(await rng.NextNumber(3_000_000_000UL, 3_000_000_002, dist), Is.InRange(3_000_000_000, 3_000_000_002)); @@ -103,7 +103,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange03Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5f, 6, dist), Is.InRange(5, 6)); Assert.That(await rng.NextNumber(0f, 1, dist), Is.InRange(0, 1)); Assert.That(await rng.NextNumber(3e9, 3e9+2, dist), Is.InRange(3e9, 3e9+2)); @@ -114,7 +114,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange04Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(10, 1, dist), Is.InRange(1, 10)); Assert.That(await rng.NextNumber(20, 1, dist), Is.InRange(1, 20)); } @@ -124,7 +124,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange04Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(10UL, 1, dist), Is.InRange(1, 10)); Assert.That(await rng.NextNumber(20UL, 1, dist), Is.InRange(1, 20)); } @@ -134,7 +134,7 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestRange04Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(10.0, 1, dist), Is.InRange(1, 10)); Assert.That(await rng.NextNumber(20.0, 1, dist), Is.InRange(1, 20)); } @@ -144,8 +144,8 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TestStoppingProducers01() { - var rng = new MultiThreadedRng(); - rng.StopProducer(); + using var rng2 = new MultiThreadedRng(); + rng2.StopProducer(); var masterToken = new CancellationTokenSource(TimeSpan.FromSeconds(16)).Token; var wasCanceled = false; @@ -153,7 +153,7 @@ namespace FastRngTests.Double while(true) { var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.GetUniform(tokenSource.Token); + await rng2.GetUniform(tokenSource.Token); if (tokenSource.IsCancellationRequested) { wasCanceled = true; @@ -170,19 +170,19 @@ namespace FastRngTests.Double Assert.That(wasCanceled, Is.True, "The consumer was not canceled"); var tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(-1d, 1d, new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(-1d, 1d, new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(0u, 6u, new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(0u, 6u, new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(0ul, 6ul, new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(0ul, 6ul, new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); } @@ -191,9 +191,9 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task OneSeed01() { - var rng1 = new MultiThreadedRng(6); - var rng2 = new MultiThreadedRng(6); - var rng3 = new MultiThreadedRng(7); + using var rng1 = new MultiThreadedRng(6); + using var rng2 = new MultiThreadedRng(6); + using var rng3 = new MultiThreadedRng(7); var rng1Sample = new double[10]; for (var n = 0; n < rng1Sample.Length; n++) @@ -206,11 +206,7 @@ namespace FastRngTests.Double var rng3Sample = new double[10]; for (var n = 0; n < rng3Sample.Length; n++) rng3Sample[n] = await rng3.GetUniform(); - - rng1.StopProducer(); - rng2.StopProducer(); - rng3.StopProducer(); - + Assert.That(rng1Sample, Is.EquivalentTo(rng2Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng3Sample)); Assert.That(rng2Sample, Is.Not.EquivalentTo(rng3Sample)); @@ -221,10 +217,10 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task TwoSeeds01() { - var rng1 = new MultiThreadedRng(3, 6); - var rng2 = new MultiThreadedRng(3, 6); - var rng3 = new MultiThreadedRng(3, 7); - var rng4 = new MultiThreadedRng(6, 3); + using var rng1 = new MultiThreadedRng(3, 6); + using var rng2 = new MultiThreadedRng(3, 6); + using var rng3 = new MultiThreadedRng(3, 7); + using var rng4 = new MultiThreadedRng(6, 3); var rng1Sample = new double[10]; for (var n = 0; n < rng1Sample.Length; n++) @@ -241,12 +237,7 @@ namespace FastRngTests.Double var rng4Sample = new double[10]; for (var n = 0; n < rng4Sample.Length; n++) rng4Sample[n] = await rng4.GetUniform(); - - rng1.StopProducer(); - rng2.StopProducer(); - rng3.StopProducer(); - rng4.StopProducer(); - + Assert.That(rng1Sample, Is.EquivalentTo(rng2Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng3Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng4Sample)); @@ -260,9 +251,9 @@ namespace FastRngTests.Double [Category(TestCategories.NORMAL)] public async Task NoSeed01() { - var rng1 = new MultiThreadedRng(); - var rng2 = new MultiThreadedRng(); - var rng3 = new MultiThreadedRng(); + using var rng1 = new MultiThreadedRng(); + using var rng2 = new MultiThreadedRng(); + using var rng3 = new MultiThreadedRng(); var rng1Sample = new double[10]; for (var n = 0; n < rng1Sample.Length; n++) @@ -275,11 +266,7 @@ namespace FastRngTests.Double var rng3Sample = new double[10]; for (var n = 0; n < rng3Sample.Length; n++) rng3Sample[n] = await rng3.GetUniform(); - - rng1.StopProducer(); - rng2.StopProducer(); - rng3.StopProducer(); - + Assert.That(rng1Sample, Is.Not.EquivalentTo(rng2Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng3Sample)); Assert.That(rng2Sample, Is.Not.EquivalentTo(rng3Sample)); @@ -295,7 +282,7 @@ namespace FastRngTests.Double tokenSource.Cancel(); using var rng2 = new MultiThreadedRng(); - var dist = new Uniform(); + var dist = new Uniform(rng2); Assert.That(await rng2.NextNumber(1, 100_000, dist, token), Is.EqualTo(0)); } diff --git a/FastRngTests/Double/PerformanceTests.cs b/FastRngTests/Double/PerformanceTests.cs index 9e20ed1..7f23c98 100644 --- a/FastRngTests/Double/PerformanceTests.cs +++ b/FastRngTests/Double/PerformanceTests.cs @@ -39,7 +39,7 @@ namespace FastRngTests.Double public async Task Generate1MNormal() { using var rng = new MultiThreadedRng(); - var dist = new NormalS02M05(); + var dist = new NormalS02M05(rng); var data = new double[1_000_000]; var stopwatch = new Stopwatch(); Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator @@ -49,7 +49,6 @@ namespace FastRngTests.Double data[n] = await rng.NextNumber(dist); stopwatch.Stop(); - rng.StopProducer(); TestContext.WriteLine($"Generated 1M normal distributed random numbers in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds."); } @@ -59,7 +58,7 @@ namespace FastRngTests.Double public async Task Generate1MChiSquare() { using var rng = new MultiThreadedRng(); - var dist = new ChiSquareK4(); + var dist = new ChiSquareK4(rng); var data = new double[1_000_000]; var stopwatch = new Stopwatch(); Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator diff --git a/FastRngTests/Float/Distributions/BetaA2B2.cs b/FastRngTests/Float/Distributions/BetaA2B2.cs index 68739f2..9bcc01a 100644 --- a/FastRngTests/Float/Distributions/BetaA2B2.cs +++ b/FastRngTests/Float/Distributions/BetaA2B2.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; @@ -14,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaDistribution01() { - var dist = new FastRng.Float.Distributions.BetaA2B2(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.BetaA2B2(rng); var fqa = new Float.FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0396f).Within(0.3f)); @@ -48,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.BetaA2B2(); + var dist = new FastRng.Float.Distributions.BetaA2B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -64,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.BetaA2B2(); + var dist = new FastRng.Float.Distributions.BetaA2B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestBetaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.BetaA2B2 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -95,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.BetaA2B2(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.BetaA2B2(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/BetaA2B5.cs b/FastRngTests/Float/Distributions/BetaA2B5.cs index f70eef1..4a51d9c 100644 --- a/FastRngTests/Float/Distributions/BetaA2B5.cs +++ b/FastRngTests/Float/Distributions/BetaA2B5.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; @@ -14,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaDistribution01() { - var dist = new FastRng.Float.Distributions.BetaA2B5(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.BetaA2B5(rng); var fqa = new Float.FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.11719271f).Within(0.3f)); @@ -48,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.BetaA2B5(); + var dist = new FastRng.Float.Distributions.BetaA2B5(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -64,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.BetaA2B5(); + var dist = new FastRng.Float.Distributions.BetaA2B5(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestBetaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.BetaA2B5 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -95,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.BetaA2B5(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.BetaA2B5(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/BetaA5B2.cs b/FastRngTests/Float/Distributions/BetaA5B2.cs index 462f5c9..15a6f60 100644 --- a/FastRngTests/Float/Distributions/BetaA5B2.cs +++ b/FastRngTests/Float/Distributions/BetaA5B2.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaDistribution01() { - var dist = new FastRng.Float.Distributions.BetaA5B2(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.BetaA5B2(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000001f).Within(0.0000003f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.BetaA5B2(); + var dist = new FastRng.Float.Distributions.BetaA5B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestBetaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.BetaA5B2(); + var dist = new FastRng.Float.Distributions.BetaA5B2(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestBetaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.BetaA5B2 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.BetaA5B2(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.BetaA5B2(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/CauchyLorentzX0.cs b/FastRngTests/Float/Distributions/CauchyLorentzX0.cs index 3d37fb5..76b49aa 100644 --- a/FastRngTests/Float/Distributions/CauchyLorentzX0.cs +++ b/FastRngTests/Float/Distributions/CauchyLorentzX0.cs @@ -18,14 +18,13 @@ namespace FastRngTests.Float.Distributions // The properties of the cauchy distribution cannot be tested by mean, media or variance, // cf. https://en.wikipedia.org/wiki/Cauchy_distribution#Explanation_of_undefined_moments - var dist = new FastRng.Float.Distributions.CauchyLorentzX0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.CauchyLorentzX0(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.976990739772031f).Within(0.06f)); @@ -52,13 +51,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.CauchyLorentzX0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -68,30 +66,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.CauchyLorentzX0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestCauchyGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.CauchyLorentzX0 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -99,11 +79,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.CauchyLorentzX0(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.CauchyLorentzX0(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/CauchyLorentzX1.cs b/FastRngTests/Float/Distributions/CauchyLorentzX1.cs index 364ddfb..51856f5 100644 --- a/FastRngTests/Float/Distributions/CauchyLorentzX1.cs +++ b/FastRngTests/Float/Distributions/CauchyLorentzX1.cs @@ -18,14 +18,13 @@ namespace FastRngTests.Float.Distributions // The properties of the cauchy distribution cannot be tested by mean, media or variance, // cf. https://en.wikipedia.org/wiki/Cauchy_distribution#Explanation_of_undefined_moments - var dist = new FastRng.Float.Distributions.CauchyLorentzX1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.CauchyLorentzX1(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.009966272570142f).Within(0.003f)); @@ -52,13 +51,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.CauchyLorentzX0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -68,30 +66,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestCauchyGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.CauchyLorentzX0(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.CauchyLorentzX0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestCauchyGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.CauchyLorentzX0 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -99,11 +79,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.CauchyLorentzX1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.CauchyLorentzX1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/ChiSquareK1.cs b/FastRngTests/Float/Distributions/ChiSquareK1.cs index 6e0dde9..97b5e53 100644 --- a/FastRngTests/Float/Distributions/ChiSquareK1.cs +++ b/FastRngTests/Float/Distributions/ChiSquareK1.cs @@ -15,17 +15,15 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareDistribution01() { - var dist = new FastRng.Float.Distributions.ChiSquareK1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK1(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) { var value = await rng.NextNumber(dist); fqa.CountThis(value); } - - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); @@ -53,13 +51,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.ChiSquareK1(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK1(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -69,13 +66,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.ChiSquareK1(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK1(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -83,28 +79,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestChiSquareGeneratorWithRange03() + public void NoRandomNumberGenerator01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.ChiSquareK1 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() - { - var dist = new FastRng.Float.Distributions.ChiSquareK1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.ChiSquareK1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/ChiSquareK10.cs b/FastRngTests/Float/Distributions/ChiSquareK10.cs index bd73119..cb610ce 100644 --- a/FastRngTests/Float/Distributions/ChiSquareK10.cs +++ b/FastRngTests/Float/Distributions/ChiSquareK10.cs @@ -15,17 +15,15 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareDistribution01() { - var dist = new FastRng.Float.Distributions.ChiSquareK10(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK10(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) { var value = await rng.NextNumber(dist); fqa.CountThis(value); } - - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); @@ -53,13 +51,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.ChiSquareK10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK10(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -69,30 +66,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.ChiSquareK10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK10(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestChiSquareGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.ChiSquareK10 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -100,11 +79,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.ChiSquareK10(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.ChiSquareK10(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/ChiSquareK4.cs b/FastRngTests/Float/Distributions/ChiSquareK4.cs index 15ec668..8f5ce9b 100644 --- a/FastRngTests/Float/Distributions/ChiSquareK4.cs +++ b/FastRngTests/Float/Distributions/ChiSquareK4.cs @@ -15,15 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareDistribution01() { - var dist = new FastRng.Float.Distributions.ChiSquareK4(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK4(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); - var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.016417705906679f).Within(0.02f)); @@ -50,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.ChiSquareK4(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK4(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -66,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestChiSquareGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.ChiSquareK4(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ChiSquareK4(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestChiSquareGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.ChiSquareK4 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -97,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.ChiSquareK4(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.ChiSquareK4(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/ExponentialLa10.cs b/FastRngTests/Float/Distributions/ExponentialLa10.cs index 213684f..e4eab8c 100644 --- a/FastRngTests/Float/Distributions/ExponentialLa10.cs +++ b/FastRngTests/Float/Distributions/ExponentialLa10.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Float.Distributions.ExponentialLa10(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ExponentialLa10(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.00075018434777f).Within(0.05f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.ExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ExponentialLa10(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.ExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ExponentialLa10(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.ExponentialLa10 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.ExponentialLa10(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.ExponentialLa10(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/ExponentialLa5.cs b/FastRngTests/Float/Distributions/ExponentialLa5.cs index e0c88bd..307b9cc 100644 --- a/FastRngTests/Float/Distributions/ExponentialLa5.cs +++ b/FastRngTests/Float/Distributions/ExponentialLa5.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Float.Distributions.ExponentialLa5(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ExponentialLa5(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.0002177398625f).Within(0.05f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.ExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ExponentialLa5(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.ExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.ExponentialLa5(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.ExponentialLa5 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.ExponentialLa5(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.ExponentialLa5(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/GammaA5B15.cs b/FastRngTests/Float/Distributions/GammaA5B15.cs index ae84e66..4be7f92 100644 --- a/FastRngTests/Float/Distributions/GammaA5B15.cs +++ b/FastRngTests/Float/Distributions/GammaA5B15.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestGammaDistribution01() { - var dist = new FastRng.Float.Distributions.GammaA5B15(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.GammaA5B15(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000929594237282f).Within(0.0008f)); @@ -44,35 +43,17 @@ namespace FastRngTests.Float.Distributions Assert.That(result[99], Is.EqualTo(0.0033038503429554f).Within(0.008f)); } - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestGammaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.GammaA5B15 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] public async Task TestGammaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.GammaA5B15(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.GammaA5B15(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -82,13 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestGammaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.GammaA5B15(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.GammaA5B15(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.GammaA5B15(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.GammaA5B15(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/InverseExponentialLa10.cs b/FastRngTests/Float/Distributions/InverseExponentialLa10.cs index 63767a7..c37e030 100644 --- a/FastRngTests/Float/Distributions/InverseExponentialLa10.cs +++ b/FastRngTests/Float/Distributions/InverseExponentialLa10.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa10(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseExponentialLa10(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000501746820562f).Within(0.0003f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseExponentialLa10(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa10(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseExponentialLa10(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.InverseExponentialLa10 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa10(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.InverseExponentialLa10(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/InverseExponentialLa5.cs b/FastRngTests/Float/Distributions/InverseExponentialLa5.cs index c525565..c927d8f 100644 --- a/FastRngTests/Float/Distributions/InverseExponentialLa5.cs +++ b/FastRngTests/Float/Distributions/InverseExponentialLa5.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialDistribution01() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa5(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseExponentialLa5(rng); var fqa = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fqa.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fqa.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.007083408929052f).Within(0.008f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange01() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseExponentialLa5(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,28 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestExponentialGeneratorWithRange02() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa5(); - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseExponentialLa5(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task TestExponentialGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.InverseExponentialLa5 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -94,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.InverseExponentialLa5(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.InverseExponentialLa5(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/InverseGammaA3B05.cs b/FastRngTests/Float/Distributions/InverseGammaA3B05.cs index 909d6ad..99756ad 100644 --- a/FastRngTests/Float/Distributions/InverseGammaA3B05.cs +++ b/FastRngTests/Float/Distributions/InverseGammaA3B05.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestInverseGammaDistribution01() { - var dist = new FastRng.Float.Distributions.InverseGammaA3B05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseGammaA3B05(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0000000000000003f).Within(0.0000001f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestInverseGammaGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.InverseGammaA3B05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseGammaA3B05(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestInverseGammaGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.InverseGammaA3B05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.InverseGammaA3B05(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestInverseGammaGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.InverseGammaA3B05 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.InverseGammaA3B05(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.InverseGammaA3B05(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/LaplaceB01M0.cs b/FastRngTests/Float/Distributions/LaplaceB01M0.cs index 4f4c15b..86aadc0 100644 --- a/FastRngTests/Float/Distributions/LaplaceB01M0.cs +++ b/FastRngTests/Float/Distributions/LaplaceB01M0.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceDistribution01() { - var dist = new FastRng.Float.Distributions.LaplaceB01M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LaplaceB01M0(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.0000000000000000f).Within(0.05f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LaplaceB01M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LaplaceB01M0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LaplaceB01M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LaplaceB01M0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestLaplaceGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LaplaceB01M0 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.LaplaceB01M0(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.LaplaceB01M0(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/LaplaceB01M05.cs b/FastRngTests/Float/Distributions/LaplaceB01M05.cs index 9b39b58..2f37393 100644 --- a/FastRngTests/Float/Distributions/LaplaceB01M05.cs +++ b/FastRngTests/Float/Distributions/LaplaceB01M05.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceDistribution01() { - var dist = new FastRng.Float.Distributions.LaplaceB01M05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LaplaceB01M05(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.0074465830709244f).Within(0.004f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LaplaceB01M05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LaplaceB01M05(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLaplaceGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LaplaceB01M05(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LaplaceB01M05(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestLaplaceGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LaplaceB01M05 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.LaplaceB01M05(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.LaplaceB01M05(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/LogNormalS1M0.cs b/FastRngTests/Float/Distributions/LogNormalS1M0.cs index 482c2a3..9ce4e90 100644 --- a/FastRngTests/Float/Distributions/LogNormalS1M0.cs +++ b/FastRngTests/Float/Distributions/LogNormalS1M0.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLogNormalDistribution01() { - var dist = new FastRng.Float.Distributions.LogNormalS1M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LogNormalS1M0(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(0.001505531f).Within(0.003f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLogNormalGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LogNormalS1M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LogNormalS1M0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestLogNormalGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LogNormalS1M0(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.LogNormalS1M0(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestLogNormalGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.LogNormalS1M0 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.LogNormalS1M0(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.LogNormalS1M0(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/NormalS02M05.cs b/FastRngTests/Float/Distributions/NormalS02M05.cs index da5145d..85c9986 100644 --- a/FastRngTests/Float/Distributions/NormalS02M05.cs +++ b/FastRngTests/Float/Distributions/NormalS02M05.cs @@ -17,11 +17,11 @@ namespace FastRngTests.Float.Distributions { const float MEAN = 0.5f; const float STANDARD_DEVIATION = 0.2f; - - var dist = new FastRng.Float.Distributions.NormalS02M05(); + + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.NormalS02M05(rng); var stats = new RunningStatistics(); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) { @@ -30,7 +30,6 @@ namespace FastRngTests.Float.Distributions fra.CountThis(nextNumber); } - rng.StopProducer(); fra.NormalizeAndPlotEvents(TestContext.WriteLine); TestContext.WriteLine($"mean={MEAN} vs. {stats.Mean}"); @@ -45,13 +44,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestNormalGeneratorWithRange01() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.NormalS02M05(); + var dist = new FastRng.Float.Distributions.NormalS02M05(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -61,30 +59,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestNormalGeneratorWithRange02() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.NormalS02M05(); + var dist = new FastRng.Float.Distributions.NormalS02M05(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestNormalGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.NormalS02M05 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -92,11 +72,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.NormalS02M05(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.NormalS02M05(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/StudentTNu1.cs b/FastRngTests/Float/Distributions/StudentTNu1.cs index 2c8b053..4acd403 100644 --- a/FastRngTests/Float/Distributions/StudentTNu1.cs +++ b/FastRngTests/Float/Distributions/StudentTNu1.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestStudentTDistribution01() { - var dist = new FastRng.Float.Distributions.StudentTNu1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.StudentTNu1(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); - + for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.000000000f).Within(0.2f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestStudentTGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.StudentTNu1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.StudentTNu1(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestStudentTGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.StudentTNu1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.StudentTNu1(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestStudentTGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.StudentTNu1 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.StudentTNu1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.StudentTNu1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/Distributions/Uniform.cs b/FastRngTests/Float/Distributions/Uniform.cs index 18333eb..a4bf7fb 100644 --- a/FastRngTests/Float/Distributions/Uniform.cs +++ b/FastRngTests/Float/Distributions/Uniform.cs @@ -10,8 +10,6 @@ namespace FastRngTests.Float.Distributions [ExcludeFromCodeCoverage] public class Uniform { - private readonly IRandom rng = new MultiThreadedRng(); - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] @@ -21,10 +19,10 @@ namespace FastRngTests.Float.Distributions const float B = 1.0f; const float MEAN = 0.5f * (A + B); const float VARIANCE = (1.0f / 12.0f) * (B - A) * (B - A); - + + using var rng = new MultiThreadedRng(); var stats = new RunningStatistics(); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) { @@ -33,7 +31,6 @@ namespace FastRngTests.Float.Distributions fra.CountThis(value); } - rng.StopProducer(); fra.NormalizeAndPlotEvents(TestContext.WriteLine); fra.PlotOccurence(TestContext.WriteLine); TestContext.WriteLine($"mean={MEAN} vs. {stats.Mean}"); @@ -61,13 +58,12 @@ namespace FastRngTests.Float.Distributions const float P_HIGH = 1.0f - 0.25f * FAILURE_PROBABILITY; var samples = new float[NUM_ROUNDS]; - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); int n; for (n = 0; n != NUM_ROUNDS; ++n) samples[n] = await rng.GetUniform(); - rng.StopProducer(); Array.Sort(samples); var jMinus = 0; @@ -119,12 +115,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestUniformGeneratorWithRange01() { + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.Uniform(); + var dist = new FastRng.Float.Distributions.Uniform(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -134,43 +130,26 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestUniformGeneratorWithRange02() { + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; - var dist = new FastRng.Float.Distributions.Uniform(); + var dist = new FastRng.Float.Distributions.Uniform(rng); for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestUniformGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.Uniform { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - + [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] public async Task TestUniformGeneratorWithRange04() { - var rng = new MultiThreadedRng(); + using var rng = new MultiThreadedRng(); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.GetUniform(); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -180,7 +159,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestRange05Uint() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -195,7 +175,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestRange05Ulong() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -210,7 +191,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestRange05Float() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -224,7 +206,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestDistribution001Uint() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -237,7 +220,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestDistribution001Ulong() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -250,7 +234,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestDistribution001Float() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 1_000_000; for (var n = 0; n < runs; n++) @@ -263,7 +248,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.LONG_RUNNING)] public async Task TestDistribution002Uint() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 100_000_000; for (var n = 0; n < runs; n++) @@ -276,7 +262,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.LONG_RUNNING)] public async Task TestDistribution002Ulong() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 100_000_000; for (var n = 0; n < runs; n++) @@ -289,7 +276,8 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.LONG_RUNNING)] public async Task TestDistribution002Float() { - var dist = new FastRng.Float.Distributions.Uniform(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.Uniform(rng); var distribution = new uint[101]; var runs = 100_000_000; for (var n = 0; n < runs; n++) diff --git a/FastRngTests/Float/Distributions/WeibullK05La1.cs b/FastRngTests/Float/Distributions/WeibullK05La1.cs index e0be1b8..13ba18b 100644 --- a/FastRngTests/Float/Distributions/WeibullK05La1.cs +++ b/FastRngTests/Float/Distributions/WeibullK05La1.cs @@ -15,14 +15,13 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestWeibullDistribution01() { - var dist = new FastRng.Float.Distributions.WeibullK05La1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.WeibullK05La1(rng); var fra = new FrequencyAnalysis(); - var rng = new MultiThreadedRng(); for (var n = 0; n < 100_000; n++) fra.CountThis(await rng.NextNumber(dist)); - rng.StopProducer(); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine); Assert.That(result[0], Is.EqualTo(1.000000000f).Within(0.2f)); @@ -49,13 +48,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestWeibullGeneratorWithRange01() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.WeibullK05La1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.WeibullK05La1(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(-1.0f, 1.0f, dist); - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0f), "Min out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max out of range"); } @@ -65,30 +63,12 @@ namespace FastRngTests.Float.Distributions [Category(TestCategories.NORMAL)] public async Task TestWeibullGeneratorWithRange02() { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.WeibullK05La1(); + using var rng = new MultiThreadedRng(); + var dist = new FastRng.Float.Distributions.WeibullK05La1(rng); var samples = new float[1_000]; for (var n = 0; n < samples.Length; n++) samples[n] = await rng.NextNumber(0.0f, 1.0f, dist); - rng.StopProducer(); - Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); - Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); - } - - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public async Task TestWeibullGeneratorWithRange03() - { - var rng = new MultiThreadedRng(); - var dist = new FastRng.Float.Distributions.WeibullK05La1 { Random = rng }; // Test default parameters - - var samples = new float[1_000]; - for (var n = 0; n < samples.Length; n++) - samples[n] = await dist.GetDistributedValue(); - - rng.StopProducer(); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0f), "Min is out of range"); Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0f), "Max is out of range"); } @@ -96,11 +76,9 @@ namespace FastRngTests.Float.Distributions [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)] - public async Task NoRandomNumberGenerator01() + public void NoRandomNumberGenerator01() { - var dist = new FastRng.Float.Distributions.WeibullK05La1(); - Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); - Assert.That(await dist.GetDistributedValue(), Is.NaN); + Assert.Throws(() => new FastRng.Float.Distributions.WeibullK05La1(null)); } } } \ No newline at end of file diff --git a/FastRngTests/Float/MultiThreadedRngTests.cs b/FastRngTests/Float/MultiThreadedRngTests.cs index be7e65b..6800eaf 100644 --- a/FastRngTests/Float/MultiThreadedRngTests.cs +++ b/FastRngTests/Float/MultiThreadedRngTests.cs @@ -18,7 +18,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange01Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); for (uint n = 0; n < 1_000_000; n++) Assert.That(await rng.NextNumber(n, 100_000 + n, dist), Is.InRange(n, 100_000 + n)); } @@ -28,7 +28,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange01Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); for (ulong n = 0; n < 1_000_000; n++) Assert.That(await rng.NextNumber(n, 100_000 + n, dist), Is.InRange(n, 100_000 + n)); } @@ -38,7 +38,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange01Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); for (var n = 0.0f; n < 1e6f; n++) Assert.That(await rng.NextNumber(n, 100_000 + n, dist), Is.InRange(n, 100_000 + n)); } @@ -48,7 +48,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange02Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5, 5, dist), Is.EqualTo(5)); Assert.That(await rng.NextNumber(0, 0, dist), Is.EqualTo(0)); Assert.That(await rng.NextNumber(3_000_000_000, 3_000_000_000, dist), Is.EqualTo(3_000_000_000)); @@ -59,7 +59,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange02Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5UL, 5, dist), Is.EqualTo(5)); Assert.That(await rng.NextNumber(0UL, 0, dist), Is.EqualTo(0)); Assert.That(await rng.NextNumber(3_000_000_000UL, 3_000_000_000, dist), Is.EqualTo(3_000_000_000)); @@ -70,7 +70,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange02Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5f, 5f, dist), Is.EqualTo(5)); Assert.That(await rng.NextNumber(0f, 0f, dist), Is.EqualTo(0)); Assert.That(await rng.NextNumber(3e9f, 3e9f, dist), Is.EqualTo(3e9f)); @@ -81,7 +81,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange03Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5, 6, dist), Is.InRange(5, 6)); Assert.That(await rng.NextNumber(0, 1, dist), Is.InRange(0, 1)); Assert.That(await rng.NextNumber(3_000_000_000, 3_000_000_002, dist), Is.InRange(3_000_000_000, 3_000_000_002)); @@ -92,7 +92,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange03Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5UL, 6, dist), Is.InRange(5, 6)); Assert.That(await rng.NextNumber(0UL, 1, dist), Is.InRange(0, 1)); Assert.That(await rng.NextNumber(3_000_000_000UL, 3_000_000_002, dist), Is.InRange(3_000_000_000, 3_000_000_002)); @@ -103,7 +103,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange03Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(5f, 6f, dist), Is.InRange(5f, 6f)); Assert.That(await rng.NextNumber(0f, 1f, dist), Is.InRange(0f, 1f)); Assert.That(await rng.NextNumber(3e9f, 3e9f+2f, dist), Is.InRange(3e9f, 3e9f+2f)); @@ -114,7 +114,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange04Uint() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(10, 1, dist), Is.InRange(1, 10)); Assert.That(await rng.NextNumber(20, 1, dist), Is.InRange(1, 20)); } @@ -124,7 +124,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange04Ulong() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(10UL, 1, dist), Is.InRange(1, 10)); Assert.That(await rng.NextNumber(20UL, 1, dist), Is.InRange(1, 20)); } @@ -134,7 +134,7 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestRange04Float() { - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng.NextNumber(10.0f, 1f, dist), Is.InRange(1f, 10f)); Assert.That(await rng.NextNumber(20.0f, 1f, dist), Is.InRange(1f, 20f)); } @@ -144,8 +144,8 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TestStoppingProducers01() { - var rng = new MultiThreadedRng(); - rng.StopProducer(); + using var rng2 = new MultiThreadedRng(); + rng2.StopProducer(); var masterToken = new CancellationTokenSource(TimeSpan.FromSeconds(16)).Token; var wasCanceled = false; @@ -153,7 +153,7 @@ namespace FastRngTests.Float while(true) { var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.GetUniform(tokenSource.Token); + await rng2.GetUniform(tokenSource.Token); if (tokenSource.IsCancellationRequested) { wasCanceled = true; @@ -170,19 +170,19 @@ namespace FastRngTests.Float Assert.That(wasCanceled, Is.True, "The consumer was not canceled"); var tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(-1f, 1f, new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(-1f, 1f, new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(0u, 6u, new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(0u, 6u, new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); tokenSource2 = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - await rng.NextNumber(0ul, 6ul, new NormalS02M05(), tokenSource2.Token); + await rng2.NextNumber(0ul, 6ul, new NormalS02M05(rng2), tokenSource2.Token); Assert.That(tokenSource2.IsCancellationRequested, Is.True); } @@ -191,9 +191,9 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task OneSeed01() { - var rng1 = new MultiThreadedRng(6); - var rng2 = new MultiThreadedRng(6); - var rng3 = new MultiThreadedRng(7); + using var rng1 = new MultiThreadedRng(6); + using var rng2 = new MultiThreadedRng(6); + using var rng3 = new MultiThreadedRng(7); var rng1Sample = new float[10]; for (var n = 0; n < rng1Sample.Length; n++) @@ -206,11 +206,7 @@ namespace FastRngTests.Float var rng3Sample = new float[10]; for (var n = 0; n < rng3Sample.Length; n++) rng3Sample[n] = await rng3.GetUniform(); - - rng1.StopProducer(); - rng2.StopProducer(); - rng3.StopProducer(); - + Assert.That(rng1Sample, Is.EquivalentTo(rng2Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng3Sample)); Assert.That(rng2Sample, Is.Not.EquivalentTo(rng3Sample)); @@ -221,10 +217,10 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task TwoSeeds01() { - var rng1 = new MultiThreadedRng(3, 6); - var rng2 = new MultiThreadedRng(3, 6); - var rng3 = new MultiThreadedRng(3, 7); - var rng4 = new MultiThreadedRng(6, 3); + using var rng1 = new MultiThreadedRng(3, 6); + using var rng2 = new MultiThreadedRng(3, 6); + using var rng3 = new MultiThreadedRng(3, 7); + using var rng4 = new MultiThreadedRng(6, 3); var rng1Sample = new float[10]; for (var n = 0; n < rng1Sample.Length; n++) @@ -241,12 +237,7 @@ namespace FastRngTests.Float var rng4Sample = new float[10]; for (var n = 0; n < rng4Sample.Length; n++) rng4Sample[n] = await rng4.GetUniform(); - - rng1.StopProducer(); - rng2.StopProducer(); - rng3.StopProducer(); - rng4.StopProducer(); - + Assert.That(rng1Sample, Is.EquivalentTo(rng2Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng3Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng4Sample)); @@ -260,9 +251,9 @@ namespace FastRngTests.Float [Category(TestCategories.NORMAL)] public async Task NoSeed01() { - var rng1 = new MultiThreadedRng(); - var rng2 = new MultiThreadedRng(); - var rng3 = new MultiThreadedRng(); + using var rng1 = new MultiThreadedRng(); + using var rng2 = new MultiThreadedRng(); + using var rng3 = new MultiThreadedRng(); var rng1Sample = new float[10]; for (var n = 0; n < rng1Sample.Length; n++) @@ -275,11 +266,7 @@ namespace FastRngTests.Float var rng3Sample = new float[10]; for (var n = 0; n < rng3Sample.Length; n++) rng3Sample[n] = await rng3.GetUniform(); - - rng1.StopProducer(); - rng2.StopProducer(); - rng3.StopProducer(); - + Assert.That(rng1Sample, Is.Not.EquivalentTo(rng2Sample)); Assert.That(rng1Sample, Is.Not.EquivalentTo(rng3Sample)); Assert.That(rng2Sample, Is.Not.EquivalentTo(rng3Sample)); @@ -295,7 +282,7 @@ namespace FastRngTests.Float tokenSource.Cancel(); using var rng2 = new MultiThreadedRng(); - var dist = new Uniform(); + var dist = new Uniform(this.rng); Assert.That(await rng2.NextNumber(1, 100_000, dist, token), Is.EqualTo(0)); } diff --git a/FastRngTests/Float/PerformanceTests.cs b/FastRngTests/Float/PerformanceTests.cs index efb4742..bb2b0b6 100644 --- a/FastRngTests/Float/PerformanceTests.cs +++ b/FastRngTests/Float/PerformanceTests.cs @@ -39,7 +39,7 @@ namespace FastRngTests.Float public async Task Generate1MNormal() { using var rng = new MultiThreadedRng(); - var dist = new NormalS02M05(); + var dist = new NormalS02M05(rng); var data = new float[1_000_000]; var stopwatch = new Stopwatch(); Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator @@ -59,7 +59,7 @@ namespace FastRngTests.Float public async Task Generate1MChiSquare() { using var rng = new MultiThreadedRng(); - var dist = new ChiSquareK4(); // TODO: Check the impact, when the rng gets set on ctor + var dist = new ChiSquareK4(rng); var data = new float[1_000_000]; var stopwatch = new Stopwatch(); Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator