Added NextNumber for (0, 1)
This commit is contained in:
parent
14ea7dac4a
commit
0432dd066e
@ -6,6 +6,8 @@ namespace FastRng.Double.Distributions
|
|||||||
{
|
{
|
||||||
public sealed class Gamma : IDistribution
|
public sealed class Gamma : IDistribution
|
||||||
{
|
{
|
||||||
|
private static readonly IDistribution NORMAL_DISTRIBUTION = new Normal();
|
||||||
|
|
||||||
private double shape = 1.0;
|
private double shape = 1.0;
|
||||||
|
|
||||||
public IRandom Random { get; set; }
|
public IRandom Random { get; set; }
|
||||||
@ -35,7 +37,6 @@ namespace FastRng.Double.Distributions
|
|||||||
|
|
||||||
if (shape >= 1.0)
|
if (shape >= 1.0)
|
||||||
{
|
{
|
||||||
var distNormal = new Normal();
|
|
||||||
var d = shape - 1.0 / 3.0;
|
var d = shape - 1.0 / 3.0;
|
||||||
var c = 1.0 / Math.Sqrt(9.0 * d);
|
var c = 1.0 / Math.Sqrt(9.0 * d);
|
||||||
while(true)
|
while(true)
|
||||||
@ -43,7 +44,7 @@ namespace FastRng.Double.Distributions
|
|||||||
double x, v;
|
double x, v;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
x = await this.Random.NextNumber(0, 1, distNormal, token);
|
x = await this.Random.NextNumber(NORMAL_DISTRIBUTION, token);
|
||||||
v = 1.0 + c * x;
|
v = 1.0 + c * x;
|
||||||
}
|
}
|
||||||
while (v <= 0.0);
|
while (v <= 0.0);
|
||||||
@ -59,8 +60,7 @@ namespace FastRng.Double.Distributions
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var dist = new Gamma{ Scale = 1, Shape = 1 + this.Shape};
|
var dist = new Gamma{ Scale = 1, Shape = 1 + this.Shape};
|
||||||
|
var g = await this.Random.NextNumber(dist, token);
|
||||||
var g = await this.Random.NextNumber(0.0f, 1.0f, dist, token); // TODO: Use double
|
|
||||||
var w = await this.Random.GetUniform(token);
|
var w = await this.Random.GetUniform(token);
|
||||||
return this.Scale * g * Math.Pow(w, 1.0 / this.Shape);
|
return this.Scale * g * Math.Pow(w, 1.0 / this.Shape);
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,9 @@ namespace FastRng.Double
|
|||||||
|
|
||||||
public ValueTask<ulong> NextNumber(ulong rangeStart, ulong rangeEnd, IDistribution distribution, CancellationToken cancel = default);
|
public ValueTask<ulong> NextNumber(ulong rangeStart, ulong rangeEnd, IDistribution distribution, CancellationToken cancel = default);
|
||||||
|
|
||||||
public ValueTask<float> NextNumber(float rangeStart, float rangeEnd, IDistribution distribution, CancellationToken cancel = default);
|
public ValueTask<double> NextNumber(double rangeStart, double rangeEnd, IDistribution distribution, CancellationToken cancel = default);
|
||||||
|
|
||||||
|
public ValueTask<double> NextNumber(IDistribution distribution, CancellationToken cancel = default);
|
||||||
|
|
||||||
public void StopProducer();
|
public void StopProducer();
|
||||||
}
|
}
|
||||||
|
@ -167,7 +167,7 @@ namespace FastRng.Double
|
|||||||
return (ulong) ((distributedValue * range) + rangeStart);
|
return (ulong) ((distributedValue * range) + rangeStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask<float> NextNumber(float rangeStart, float rangeEnd, IDistribution distribution, CancellationToken cancel = default(CancellationToken))
|
public async ValueTask<double> NextNumber(double rangeStart, double rangeEnd, IDistribution distribution, CancellationToken cancel = default(CancellationToken))
|
||||||
{
|
{
|
||||||
if (rangeStart > rangeEnd)
|
if (rangeStart > rangeEnd)
|
||||||
{
|
{
|
||||||
@ -180,9 +180,11 @@ namespace FastRng.Double
|
|||||||
distribution.Random = this;
|
distribution.Random = this;
|
||||||
|
|
||||||
var distributedValue = await distribution.GetDistributedValue(cancel);
|
var distributedValue = await distribution.GetDistributedValue(cancel);
|
||||||
return (float) ((distributedValue * range) + rangeStart);
|
return (distributedValue * range) + rangeStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async ValueTask<double> NextNumber(IDistribution distribution, CancellationToken cancel = default) => await this.NextNumber(0.0, 1.0, distribution, cancel);
|
||||||
|
|
||||||
public void StopProducer() => this.producerToken.Cancel();
|
public void StopProducer() => this.producerToken.Cancel();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user