diff --git a/FastRng/Double/Distributions/BetaA2B2.cs b/FastRng/Double/Distributions/BetaA2B2.cs index 94ffcb0..e8dd98d 100644 --- a/FastRng/Double/Distributions/BetaA2B2.cs +++ b/FastRng/Double/Distributions/BetaA2B2.cs @@ -4,37 +4,12 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class BetaA2B2 : IDistribution + public sealed class BetaA2B2 : Distribution { private const double ALPHA = 2; private const double BETA = 2; private const double CONSTANT = 4; - - private ShapeFitter fitter; - private IRandom random; - public BetaA2B2() - { - } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(BetaA2B2.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * Math.Pow(x, ALPHA - 1) * Math.Pow(1 - x, BETA - 1); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 3f5febd..6290e33 100644 --- a/FastRng/Double/Distributions/BetaA2B5.cs +++ b/FastRng/Double/Distributions/BetaA2B5.cs @@ -4,37 +4,12 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class BetaA2B5 : IDistribution + public sealed class BetaA2B5 : Distribution { private const double ALPHA = 2; private const double BETA = 5; private const double CONSTANT = 12.2; - - private ShapeFitter fitter; - private IRandom random; - public BetaA2B5() - { - } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(BetaA2B5.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * Math.Pow(x, ALPHA - 1) * Math.Pow(1 - x, BETA - 1); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 df3d6d9..49d289a 100644 --- a/FastRng/Double/Distributions/BetaA5B2.cs +++ b/FastRng/Double/Distributions/BetaA5B2.cs @@ -4,37 +4,12 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class BetaA5B2 : IDistribution + public sealed class BetaA5B2 : Distribution { private const double ALPHA = 5; private const double BETA = 2; private const double CONSTANT = 12.2; - - private ShapeFitter fitter; - private IRandom random; - public BetaA5B2() - { - } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(BetaA5B2.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * Math.Pow(x, ALPHA - 1) * Math.Pow(1 - x, BETA - 1); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 f04c4bd..43a4abd 100644 --- a/FastRng/Double/Distributions/CauchyLorentzX0.cs +++ b/FastRng/Double/Distributions/CauchyLorentzX0.cs @@ -4,33 +4,12 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class CauchyLorentzX0 : IDistribution + public sealed class CauchyLorentzX0 : Distribution { private const double CONSTANT = 0.31; private const double SCALE = 0.1; private const double MEDIAN = 0.0; - - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(CauchyLorentzX0.ShapeFunction, this.random, 100); - } - } - private static double ShapeFunction(double x) => CONSTANT * (1.0 / (Math.PI * SCALE)) * ((SCALE * SCALE) / (Math.Pow(x - MEDIAN, 2) + (SCALE * SCALE))); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 eea6e5b..64a601c 100644 --- a/FastRng/Double/Distributions/CauchyLorentzX1.cs +++ b/FastRng/Double/Distributions/CauchyLorentzX1.cs @@ -4,33 +4,12 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class CauchyLorentzX1 : IDistribution + public sealed class CauchyLorentzX1 : Distribution { private const double CONSTANT = 0.31; private const double SCALE = 0.1; private const double MEDIAN = 1.0; - - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(CauchyLorentzX1.ShapeFunction, this.random, 100); - } - } - private static double ShapeFunction(double x) => CONSTANT * (1.0 / (Math.PI * SCALE)) * ((SCALE * SCALE) / (Math.Pow(x - MEDIAN, 2) + (SCALE * SCALE))); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 4cae567..41723df 100644 --- a/FastRng/Double/Distributions/ChiSquareK1.cs +++ b/FastRng/Double/Distributions/ChiSquareK1.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class ChiSquareK1 : IDistribution + public sealed class ChiSquareK1 : Distribution { private const double K = 1.0; private const double K_HALF = K * 0.5d; @@ -13,34 +13,13 @@ namespace FastRng.Double.Distributions private static readonly double DIVISOR; - private ShapeFitter fitter; - private IRandom random; - static ChiSquareK1() { var twoToTheKHalf = Math.Pow(2, K_HALF); var gammaKHalf = MathTools.Gamma(K_HALF); DIVISOR = twoToTheKHalf * gammaKHalf; } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(ChiSquareK1.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * ((Math.Pow(x, K_HALF_MINUS_ONE) * Math.Exp(-x * 0.5d)) / DIVISOR); - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 532da97..c91b4df 100644 --- a/FastRng/Double/Distributions/ChiSquareK10.cs +++ b/FastRng/Double/Distributions/ChiSquareK10.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class ChiSquareK10 : IDistribution + public sealed class ChiSquareK10 : Distribution { private const double K = 10.0; private const double K_HALF = K * 0.5d; @@ -13,34 +13,13 @@ namespace FastRng.Double.Distributions private static readonly double DIVISOR; - private ShapeFitter fitter; - private IRandom random; - static ChiSquareK10() { var twoToTheKHalf = Math.Pow(2, K_HALF); var gammaKHalf = MathTools.Gamma(K_HALF); DIVISOR = twoToTheKHalf * gammaKHalf; } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(ChiSquareK10.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * ((Math.Pow(x, K_HALF_MINUS_ONE) * Math.Exp(-x * 0.5d)) / DIVISOR); - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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/ChiSquareK4.cs b/FastRng/Double/Distributions/ChiSquareK4.cs index 59178f2..7585587 100644 --- a/FastRng/Double/Distributions/ChiSquareK4.cs +++ b/FastRng/Double/Distributions/ChiSquareK4.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class ChiSquareK4 : IDistribution + public sealed class ChiSquareK4 : Distribution { private const double K = 4.0; private const double K_HALF = K * 0.5d; @@ -13,34 +13,13 @@ namespace FastRng.Double.Distributions private static readonly double DIVISOR; - private ShapeFitter fitter; - private IRandom random; - static ChiSquareK4() { var twoToTheKHalf = Math.Pow(2, K_HALF); var gammaKHalf = MathTools.Gamma(K_HALF); DIVISOR = twoToTheKHalf * gammaKHalf; } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(ChiSquareK4.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * ((Math.Pow(x, K_HALF_MINUS_ONE) * Math.Exp(-x * 0.5d)) / DIVISOR); - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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/Distribution.cs b/FastRng/Double/Distributions/Distribution.cs new file mode 100644 index 0000000..f96b6ae --- /dev/null +++ b/FastRng/Double/Distributions/Distribution.cs @@ -0,0 +1,34 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace FastRng.Double.Distributions +{ + public abstract class Distribution : IDistribution + { + private ShapeFitter fitter; + private IRandom random; + + public IRandom Random + { + get => this.random; + set + { + if(this.random != null) + return; + + this.random = value; + 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); + } + } +} \ No newline at end of file diff --git a/FastRng/Double/Distributions/ExponentialLa10.cs b/FastRng/Double/Distributions/ExponentialLa10.cs index ee6a1e6..47a4e46 100644 --- a/FastRng/Double/Distributions/ExponentialLa10.cs +++ b/FastRng/Double/Distributions/ExponentialLa10.cs @@ -4,32 +4,11 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class ExponentialLa10 : IDistribution + public sealed class ExponentialLa10 : Distribution { private const double LAMBDA = 10.0; private const double CONSTANT = 0.1106; - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(ExponentialLa10.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(-LAMBDA * x); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 e3fe405..9ccc806 100644 --- a/FastRng/Double/Distributions/ExponentialLa5.cs +++ b/FastRng/Double/Distributions/ExponentialLa5.cs @@ -4,32 +4,11 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class ExponentialLa5 : IDistribution + public sealed class ExponentialLa5 : Distribution { private const double LAMBDA = 5.0; private const double CONSTANT = 0.2103; - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(ExponentialLa5.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(-LAMBDA * x); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 36fc44c..6a9b455 100644 --- a/FastRng/Double/Distributions/GammaA5B15.cs +++ b/FastRng/Double/Distributions/GammaA5B15.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class GammaA5B15 : IDistribution + public sealed class GammaA5B15 : Distribution { private const double ALPHA = 5.0; private const double BETA = 15.0; @@ -13,33 +13,12 @@ namespace FastRng.Double.Distributions private static readonly double GAMMA_ALPHA; private static readonly double BETA_TO_THE_ALPHA; - private ShapeFitter fitter; - private IRandom random; - static GammaA5B15() { GAMMA_ALPHA = MathTools.Gamma(ALPHA); BETA_TO_THE_ALPHA = Math.Pow(BETA, ALPHA); } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(GammaA5B15.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * ((BETA_TO_THE_ALPHA * Math.Pow(x, ALPHA - 1.0d) * Math.Exp(-BETA * x)) / GAMMA_ALPHA); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + + protected override double ShapeFunction(double x) => CONSTANT * ((BETA_TO_THE_ALPHA * Math.Pow(x, ALPHA - 1.0d) * Math.Exp(-BETA * x)) / GAMMA_ALPHA); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/InverseExponentialLa10.cs b/FastRng/Double/Distributions/InverseExponentialLa10.cs index 98b8d3e..61d1397 100644 --- a/FastRng/Double/Distributions/InverseExponentialLa10.cs +++ b/FastRng/Double/Distributions/InverseExponentialLa10.cs @@ -4,32 +4,11 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class InverseExponentialLa10 : IDistribution + public sealed class InverseExponentialLa10 : Distribution { private const double LAMBDA = 10.0; private const double CONSTANT = 4.539992976248453e-06; - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(InverseExponentialLa10.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(LAMBDA * x); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 0318d3f..696f2c8 100644 --- a/FastRng/Double/Distributions/InverseExponentialLa5.cs +++ b/FastRng/Double/Distributions/InverseExponentialLa5.cs @@ -4,32 +4,11 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class InverseExponentialLa5 : IDistribution + public sealed class InverseExponentialLa5 : Distribution { private const double LAMBDA = 5.0; private const double CONSTANT = 0.001347589399817; - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(InverseExponentialLa5.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * LAMBDA * Math.Exp(LAMBDA * x); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 83bded4..ed5a273 100644 --- a/FastRng/Double/Distributions/InverseGammaA3B05.cs +++ b/FastRng/Double/Distributions/InverseGammaA3B05.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class InverseGammaA3B05 : IDistribution + public sealed class InverseGammaA3B05 : Distribution { private const double ALPHA = 3.0; private const double BETA = 0.5; @@ -12,9 +12,6 @@ namespace FastRng.Double.Distributions private static readonly double FACTOR_LEFT; - private ShapeFitter fitter; - private IRandom random; - static InverseGammaA3B05() { var gammaAlpha = MathTools.Gamma(ALPHA); @@ -22,25 +19,7 @@ namespace FastRng.Double.Distributions FACTOR_LEFT = CONSTANT * (betaToTheAlpha / gammaAlpha); } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(InverseGammaA3B05.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => FACTOR_LEFT * Math.Pow(x, -ALPHA - 1.0d) * Math.Exp(-BETA / x); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + + protected override double ShapeFunction(double x) => FACTOR_LEFT * Math.Pow(x, -ALPHA - 1.0d) * Math.Exp(-BETA / x); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/LaplaceB01M0.cs b/FastRng/Double/Distributions/LaplaceB01M0.cs index 66127c0..9b54b32 100644 --- a/FastRng/Double/Distributions/LaplaceB01M0.cs +++ b/FastRng/Double/Distributions/LaplaceB01M0.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class LaplaceB01M0 : IDistribution + public sealed class LaplaceB01M0 : Distribution { private const double B = 0.1; private const double MU = 0.0; @@ -12,32 +12,11 @@ namespace FastRng.Double.Distributions private static readonly double FACTOR_LEFT; - private ShapeFitter fitter; - private IRandom random; - static LaplaceB01M0() { FACTOR_LEFT = CONSTANT / (2.0d * B); } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(LaplaceB01M0.ShapeFunction, this.random, 100); - } - } - private static double ShapeFunction(double x) => FACTOR_LEFT * Math.Exp(-Math.Abs(x - MU) / B); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + protected override double ShapeFunction(double x) => FACTOR_LEFT * Math.Exp(-Math.Abs(x - MU) / B); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/LaplaceB01M05.cs b/FastRng/Double/Distributions/LaplaceB01M05.cs index 2197507..d050e7c 100644 --- a/FastRng/Double/Distributions/LaplaceB01M05.cs +++ b/FastRng/Double/Distributions/LaplaceB01M05.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class LaplaceB01M05 : IDistribution + public sealed class LaplaceB01M05 : Distribution { private const double B = 0.1; private const double MU = 0.5; @@ -12,32 +12,11 @@ namespace FastRng.Double.Distributions private static readonly double FACTOR_LEFT; - private ShapeFitter fitter; - private IRandom random; - static LaplaceB01M05() { FACTOR_LEFT = CONSTANT / (2.0d * B); } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(LaplaceB01M05.ShapeFunction, this.random, 100); - } - } - private static double ShapeFunction(double x) => FACTOR_LEFT * Math.Exp(-Math.Abs(x - MU) / B); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + protected override double ShapeFunction(double x) => FACTOR_LEFT * Math.Exp(-Math.Abs(x - MU) / B); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/LogNormalS1M0.cs b/FastRng/Double/Distributions/LogNormalS1M0.cs index 2b7209b..91e0b61 100644 --- a/FastRng/Double/Distributions/LogNormalS1M0.cs +++ b/FastRng/Double/Distributions/LogNormalS1M0.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class LogNormalS1M0 : IDistribution + public sealed class LogNormalS1M0 : Distribution { private const double SIGMA = 1.0; private const double MU = 0.0; @@ -12,32 +12,11 @@ namespace FastRng.Double.Distributions private static readonly double FACTOR; - private ShapeFitter fitter; - private IRandom random; - static LogNormalS1M0() { FACTOR = SIGMA * Math.Sqrt(2 * Math.PI); } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(LogNormalS1M0.ShapeFunction, this.random, 100); - } - } - private static double ShapeFunction(double x) => (CONSTANT / (x * FACTOR)) * Math.Exp( -(Math.Pow(Math.Log(x) - MU, 2) / (2 * Math.Pow(SIGMA, 2)))); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + protected override double ShapeFunction(double x) => (CONSTANT / (x * FACTOR)) * Math.Exp( -(Math.Pow(Math.Log(x) - MU, 2) / (2 * Math.Pow(SIGMA, 2)))); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/NormalS02M05.cs b/FastRng/Double/Distributions/NormalS02M05.cs index 982b754..c08067f 100644 --- a/FastRng/Double/Distributions/NormalS02M05.cs +++ b/FastRng/Double/Distributions/NormalS02M05.cs @@ -5,37 +5,12 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class NormalS02M05 : IDistribution + public sealed class NormalS02M05 : Distribution { private const double SQRT_2_PI = 2.506628275; private const double STDDEV = 0.2; private const double MEAN = 0.5; - - private ShapeFitter fitter; - private IRandom random; - public NormalS02M05() - { - } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(NormalS02M05.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => 1.0 / (STDDEV * SQRT_2_PI) * Math.Exp(-0.5 * Math.Pow((x - MEAN) / STDDEV, 2.0)); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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 91189e3..5061b4e 100644 --- a/FastRng/Double/Distributions/StudentTNu1.cs +++ b/FastRng/Double/Distributions/StudentTNu1.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class StudentTNu1 : IDistribution + public sealed class StudentTNu1 : Distribution { private const double NU = 1.0; private const double START = 0.0; @@ -15,34 +15,13 @@ namespace FastRng.Double.Distributions private static readonly double DIVISOR; private static readonly double EXPONENT; - private ShapeFitter fitter; - private IRandom random; - static StudentTNu1() { DIVIDEND = MathTools.Gamma((NU + 1.0d) * 0.5d); DIVISOR = Math.Sqrt(NU * Math.PI) * MathTools.Gamma(NU * 0.5d); EXPONENT = -((NU + 1.0d) * 0.5d); } - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(StudentTNu1.ShapeFunction, this.random, 100); - } - } - private static double ShapeFunction(double x) => CONSTANT * Math.Pow((DIVIDEND / DIVISOR) * Math.Pow(1.0d + Math.Pow(START + x * COMPRESS, 2) / NU, EXPONENT), COMPRESS); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + protected override double ShapeFunction(double x) => CONSTANT * Math.Pow((DIVIDEND / DIVISOR) * Math.Pow(1.0d + Math.Pow(START + x * COMPRESS, 2) / NU, EXPONENT), COMPRESS); } } \ No newline at end of file diff --git a/FastRng/Double/Distributions/WeibullK05La1.cs b/FastRng/Double/Distributions/WeibullK05La1.cs index fad7460..520c3c9 100644 --- a/FastRng/Double/Distributions/WeibullK05La1.cs +++ b/FastRng/Double/Distributions/WeibullK05La1.cs @@ -4,33 +4,12 @@ using System.Threading.Tasks; namespace FastRng.Double.Distributions { - public sealed class WeibullK05La1 : IDistribution + public sealed class WeibullK05La1 : Distribution { private const double K = 0.5; private const double LAMBDA = 1.0; private const double CONSTANT = 0.221034183615129; - private ShapeFitter fitter; - private IRandom random; - - public IRandom Random - { - get => this.random; - set - { - this.random = value; - this.fitter = new ShapeFitter(WeibullK05La1.ShapeFunction, this.random, 100); - } - } - - private static double ShapeFunction(double x) => CONSTANT * ( (K / LAMBDA) * Math.Pow(x / LAMBDA, K - 1.0d) * Math.Exp(-Math.Pow(x/LAMBDA, K))); - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - return await this.fitter.NextNumber(token); - } + 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