FastRng/FastRng/Double/Distributions/Exponential.cs

36 lines
937 B
C#
Raw Normal View History

2020-09-25 22:06:14 +00:00
using System;
using System.Threading;
using System.Threading.Tasks;
2020-09-26 09:40:01 +00:00
namespace FastRng.Double.Distributions
2020-09-25 22:06:14 +00:00
{
public sealed class Exponential : IDistribution
{
private double mean = 1;
public IRandom Random { get; set; }
public double Mean
{
get => this.mean;
set
{
if(value <= 0)
throw new ArgumentOutOfRangeException(message: "Mean must be greater than 0", null);
this.mean = value;
}
}
public async Task<double> GetDistributedValue(CancellationToken token)
{
if (this.Random == null)
return 0;
if(this.Mean == 1)
2020-09-26 09:34:26 +00:00
return -Math.Log(await this.Random.GetUniform(token));
2020-09-25 22:06:14 +00:00
else
2020-09-26 09:34:26 +00:00
return this.Mean * -Math.Log(await this.Random.GetUniform(token));
2020-09-25 22:06:14 +00:00
}
}
}