From 1651fbaa2bcab796eaf641ef3d103ba15a8f3a26 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 31 Oct 2020 23:49:37 +0100 Subject: [PATCH] Added float implementations --- FastRng/Float/Distributions/BetaA2B2.cs | 13 ++++++++ FastRng/Float/Distributions/BetaA2B5.cs | 13 ++++++++ FastRng/Float/Distributions/BetaA5B2.cs | 13 ++++++++ FastRng/Float/Distributions/Distribution.cs | 34 +++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 FastRng/Float/Distributions/BetaA2B2.cs create mode 100644 FastRng/Float/Distributions/BetaA2B5.cs create mode 100644 FastRng/Float/Distributions/BetaA5B2.cs create mode 100644 FastRng/Float/Distributions/Distribution.cs diff --git a/FastRng/Float/Distributions/BetaA2B2.cs b/FastRng/Float/Distributions/BetaA2B2.cs new file mode 100644 index 0000000..d533d96 --- /dev/null +++ b/FastRng/Float/Distributions/BetaA2B2.cs @@ -0,0 +1,13 @@ +using System; + +namespace FastRng.Float.Distributions +{ + public sealed class BetaA2B2 : Distribution + { + private const float ALPHA = 2f; + private const float BETA = 2f; + private const float CONSTANT = 4f; + + 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 new file mode 100644 index 0000000..d3ea994 --- /dev/null +++ b/FastRng/Float/Distributions/BetaA2B5.cs @@ -0,0 +1,13 @@ +using System; + +namespace FastRng.Float.Distributions +{ + public sealed class BetaA2B5 : Distribution + { + private const float ALPHA = 2f; + private const float BETA = 5f; + private const float CONSTANT = 12.2f; + + 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 new file mode 100644 index 0000000..79fbc45 --- /dev/null +++ b/FastRng/Float/Distributions/BetaA5B2.cs @@ -0,0 +1,13 @@ +using System; + +namespace FastRng.Float.Distributions +{ + public sealed class BetaA5B2 : Distribution + { + private const float ALPHA = 5f; + private const float BETA = 2f; + private const float CONSTANT = 12.2f; + + 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/Distribution.cs b/FastRng/Float/Distributions/Distribution.cs new file mode 100644 index 0000000..47a86ce --- /dev/null +++ b/FastRng/Float/Distributions/Distribution.cs @@ -0,0 +1,34 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace FastRng.Float.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 float ShapeFunction(float x); + + public async ValueTask GetDistributedValue(CancellationToken token = default) + { + if (this.Random == null) + return float.NaN; + + return await this.fitter.NextNumber(token); + } + } +} \ No newline at end of file