init commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
|
||||
namespace AForge.Math.Random;
|
||||
|
||||
public class ExponentialGenerator : IRandomNumberGenerator
|
||||
{
|
||||
private UniformOneGenerator rand;
|
||||
|
||||
private float rate;
|
||||
|
||||
public float Rate => rate;
|
||||
|
||||
public float Mean => 1f / rate;
|
||||
|
||||
public float Variance => 1f / (rate * rate);
|
||||
|
||||
public ExponentialGenerator(float rate)
|
||||
: this(rate, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public ExponentialGenerator(float rate, int seed)
|
||||
{
|
||||
if (rate <= 0f)
|
||||
{
|
||||
throw new ArgumentException("Rate value should be greater than zero.");
|
||||
}
|
||||
rand = new UniformOneGenerator(seed);
|
||||
this.rate = rate;
|
||||
}
|
||||
|
||||
public float Next()
|
||||
{
|
||||
return (0f - (float)System.Math.Log(rand.Next())) / rate;
|
||||
}
|
||||
|
||||
public void SetSeed(int seed)
|
||||
{
|
||||
rand = new UniformOneGenerator(seed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace AForge.Math.Random;
|
||||
|
||||
public class GaussianGenerator : IRandomNumberGenerator
|
||||
{
|
||||
private StandardGenerator rand;
|
||||
|
||||
private float mean;
|
||||
|
||||
private float stdDev;
|
||||
|
||||
public float Mean => mean;
|
||||
|
||||
public float Variance => stdDev * stdDev;
|
||||
|
||||
public float StdDev => stdDev;
|
||||
|
||||
public GaussianGenerator(float mean, float stdDev)
|
||||
: this(mean, stdDev, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public GaussianGenerator(float mean, float stdDev, int seed)
|
||||
{
|
||||
this.mean = mean;
|
||||
this.stdDev = stdDev;
|
||||
rand = new StandardGenerator(seed);
|
||||
}
|
||||
|
||||
public float Next()
|
||||
{
|
||||
return rand.Next() * stdDev + mean;
|
||||
}
|
||||
|
||||
public void SetSeed(int seed)
|
||||
{
|
||||
rand = new StandardGenerator(seed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace AForge.Math.Random;
|
||||
|
||||
public interface IRandomNumberGenerator
|
||||
{
|
||||
float Mean { get; }
|
||||
|
||||
float Variance { get; }
|
||||
|
||||
float Next();
|
||||
|
||||
void SetSeed(int seed);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
|
||||
namespace AForge.Math.Random;
|
||||
|
||||
public class StandardGenerator : IRandomNumberGenerator
|
||||
{
|
||||
private UniformOneGenerator rand;
|
||||
|
||||
private float secondValue;
|
||||
|
||||
private bool useSecond;
|
||||
|
||||
public float Mean => 0f;
|
||||
|
||||
public float Variance => 1f;
|
||||
|
||||
public StandardGenerator()
|
||||
{
|
||||
rand = new UniformOneGenerator();
|
||||
}
|
||||
|
||||
public StandardGenerator(int seed)
|
||||
{
|
||||
rand = new UniformOneGenerator(seed);
|
||||
}
|
||||
|
||||
public float Next()
|
||||
{
|
||||
if (useSecond)
|
||||
{
|
||||
useSecond = false;
|
||||
return secondValue;
|
||||
}
|
||||
float num;
|
||||
float num2;
|
||||
float num3;
|
||||
do
|
||||
{
|
||||
num = rand.Next() * 2f - 1f;
|
||||
num2 = rand.Next() * 2f - 1f;
|
||||
num3 = num * num + num2 * num2;
|
||||
}
|
||||
while (num3 >= 1f);
|
||||
num3 = (float)System.Math.Sqrt(-2.0 * System.Math.Log(num3) / (double)num3);
|
||||
float result = num * num3;
|
||||
secondValue = num2 * num3;
|
||||
useSecond = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
public void SetSeed(int seed)
|
||||
{
|
||||
rand = new UniformOneGenerator(seed);
|
||||
useSecond = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace AForge.Math.Random;
|
||||
|
||||
public class UniformGenerator : IRandomNumberGenerator
|
||||
{
|
||||
private UniformOneGenerator rand;
|
||||
|
||||
private float min;
|
||||
|
||||
private float length;
|
||||
|
||||
public float Mean => (min + min + length) / 2f;
|
||||
|
||||
public float Variance => length * length / 12f;
|
||||
|
||||
public Range Range => new Range(min, min + length);
|
||||
|
||||
public UniformGenerator(Range range)
|
||||
: this(range, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public UniformGenerator(Range range, int seed)
|
||||
{
|
||||
rand = new UniformOneGenerator(seed);
|
||||
min = range.Min;
|
||||
length = range.Length;
|
||||
}
|
||||
|
||||
public float Next()
|
||||
{
|
||||
return rand.Next() * length + min;
|
||||
}
|
||||
|
||||
public void SetSeed(int seed)
|
||||
{
|
||||
rand = new UniformOneGenerator(seed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace AForge.Math.Random;
|
||||
|
||||
public class UniformOneGenerator : IRandomNumberGenerator
|
||||
{
|
||||
private ThreadSafeRandom rand;
|
||||
|
||||
public float Mean => 0.5f;
|
||||
|
||||
public float Variance => 1f / 12f;
|
||||
|
||||
public UniformOneGenerator()
|
||||
{
|
||||
rand = new ThreadSafeRandom(0);
|
||||
}
|
||||
|
||||
public UniformOneGenerator(int seed)
|
||||
{
|
||||
rand = new ThreadSafeRandom(seed);
|
||||
}
|
||||
|
||||
public float Next()
|
||||
{
|
||||
return (float)rand.NextDouble();
|
||||
}
|
||||
|
||||
public void SetSeed(int seed)
|
||||
{
|
||||
rand = new ThreadSafeRandom(seed);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user