Added float implementations

This commit is contained in:
Thorsten Sommer 2020-10-31 23:49:37 +01:00
parent 706f2552bc
commit 1651fbaa2b
4 changed files with 73 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<float> GetDistributedValue(CancellationToken token = default)
{
if (this.Random == null)
return float.NaN;
return await this.fitter.NextNumber(token);
}
}
}