diff --git a/FastRng/Double/Distributions/CauchyLorentz.cs b/FastRng/Double/Distributions/CauchyLorentz.cs index 936d6f0..f0f35b2 100644 --- a/FastRng/Double/Distributions/CauchyLorentz.cs +++ b/FastRng/Double/Distributions/CauchyLorentz.cs @@ -6,31 +6,31 @@ namespace FastRng.Double.Distributions { public sealed class CauchyLorentz : IDistribution { - private double scale = 1.0; + private const double CONSTANT = 0.31; + private const double SCALE = 0.1; + private const double MEDIAN = 0.0; - public IRandom Random { get; set; } + private ShapeFitter fitter; + private IRandom random; - public double Scale + public IRandom Random { - get => this.scale; + get => this.random; set { - if(value <= 0.0) - throw new ArgumentOutOfRangeException(message: "Scale must be greater than 0", null); - - this.scale = value; + this.random = value; + this.fitter = new ShapeFitter(CauchyLorentz.ShapeFunction, this.random, 50, 0.98); } } - public double Median { get; set; } = 0.0; - + 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; - - var value = await this.Random.GetUniform(token); - return 1.0 / (Math.PI * this.Scale * (1 + Math.Pow((value - this.Median) / this.Scale, 2))); + + return await this.fitter.NextNumber(token); } } } \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/CauchyLorentz.cs b/FastRngTests/Double/Distributions/CauchyLorentz.cs index 9fbcc09..c2b1cfa 100644 --- a/FastRngTests/Double/Distributions/CauchyLorentz.cs +++ b/FastRngTests/Double/Distributions/CauchyLorentz.cs @@ -63,23 +63,6 @@ namespace FastRngTests.Double.Distributions Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); } - [Test] - [Category(TestCategories.COVER)] - [Category(TestCategories.NORMAL)] - public void ParameterTest01() - { - var dist = new FastRng.Double.Distributions.CauchyLorentz(); - - Assert.Throws(() => dist.Scale = 0); - Assert.Throws(() => dist.Scale = -78); - Assert.DoesNotThrow(() => dist.Scale = 0.0001); - Assert.DoesNotThrow(() => dist.Scale = 4); - - Assert.DoesNotThrow(() => dist.Median = -45); - Assert.DoesNotThrow(() => dist.Median = 15); - Assert.DoesNotThrow(() => dist.Median = 0); - } - [Test] [Category(TestCategories.COVER)] [Category(TestCategories.NORMAL)]