Box-Muller transform是從高斯分佈中採樣隨機值的優雅且合理的高性能方法。從高斯分佈中抽取隨機值的最快方法是什麼?
我正在尋找一種清晰寫入並以C#編寫的更快速的方法。
僅供參考這裏的箱穆勒執行充當性能比較基準的實現......
public class GaussianGenerator
{
FastRandom _rng = new FastRandom();
double? _spareValue = null;
/// <summary>
/// Get the next sample point from the gaussian distribution.
/// </summary>
public double NextDouble()
{
if(null != _spareValue)
{
double tmp = _spareValue.Value;
_spareValue = null;
return tmp;
}
// Generate two new gaussian values.
double x, y, sqr;
// We need a non-zero random point inside the unit circle.
do
{
x = 2.0 * _rng.NextDouble() - 1.0;
y = 2.0 * _rng.NextDouble() - 1.0;
sqr = x * x + y * y;
}
while(sqr > 1.0 || sqr == 0);
// Make the Box-Muller transformation.
double fac = Math.Sqrt(-2.0 * Math.Log(sqr)/sqr);
_spareValue = x * fac;
return y * fac;
}
/// <summary>
/// Get the next sample point from the gaussian distribution.
/// </summary>
public double NextDouble(double mu, double sigma)
{
return mu + (NextDouble() * sigma);
}
}
您有問題嗎? –
您是否在C#中使用了Ziggurat算法? – redcalx
你想要速度和優雅?實行制服比例! Ziggurat在我看來是醜陋而難以調整的。 –