如何使用魏布尔分布中的随机数?

时间:2019-01-31 10:03:11

标签: c# random mathnet weibull

我正在尝试将Weibull分布的随机数放在一个圆的扇区中。

我生成了随机数,然后将其分配为Weibull分布。现在,我要使用这些Weibull分布式随机数。

RadarSpace GetWeibullClutter()
{ 
    Random _randomNumberGenerator = new Random();
    Weibull myweibull = new Weibull(3,2,_randomNumberGenerator);

    int n = 50; // number of clutter elements
    var maxRange = _detectionModel.MaximumRange;
    var centreX = 0; // Centre of the field of view -- X coordinates
    var centreY = 0; // Centre of the field of view -- Y coordinates

    var minimumAngle = Math.PI / 4; // _detectionModel.MinimumPhi;
    var maximumAngle = (3 * Math.PI) / 4; // _detectionModel.MaximumPhir;

    var theta = (maximumAngle - minimumAngle) * myweibull + minimumAngle;
    var r = maxRange * Math.Sqrt(_randomNumberGenerator.Next(n));

    var x = centreX + r * Math.Cos(theta);
    var y = centreY + r * Math.Sin(theta);

我希望将Weibull分布的随机数乘以var theta,但宁可说

  

运算符'*'不适用于'double'和'weibull'类型的操作数

更新的代码是

RadarSpace GetWeibullClutter()
            { 
                Random _randomNumberGenerator = new Random();
                Weibull myweibull = new Weibull(3,2,_randomNumberGenerator);

                int n = 50; // number of clutter elements
                var maxRange = _detectionModel.MaximumRange;
                var centreX = 0; // Centre of the field of view -- X coordinates
                var centreY = 0; // Centre of the field of view -- Y coordinates

                var minimumAngle = Math.PI / 4; // _detectionModel.MinimumPhi;
                var maximumAngle = (3 * Math.PI) / 4; // _detectionModel.MaximumPhir;

                var theta = 0.0;
                var r = 0.0;

                var randomNumbers = new double[n];
                myweibull.Samples(randomNumbers);
                for (int i = 0; i < n; i++)
                {
                    theta = (maximumAngle - minimumAngle) * randomNumbers[i] + minimumAngle;
                    r = maxRange * Math.Sqrt(randomNumbers[i]);
                }
                //var theta = (maximumAngle - minimumAngle) * myweibull.Sample() + minimumAngle;
                //var r = maxRange * Math.Sqrt(_randomNumberGenerator.Next(n));

                var x = centreX + r * Math.Cos(theta);
                var y = centreY + r * Math.Sin(theta);

1 个答案:

答案 0 :(得分:1)

您似乎正在使用Math.NET Numerics库。 Weibull distribution class实现了IContinuousDistribution接口,该接口提供了属性和方法:

double Mode { get; }
double Minimum { get; }
double Maximum { get; }
double Density(double x);
double DensityLn(double x);
double Sample();
void Samples(double[] values);
IEnumerable<double> Samples();

您的变量myweibull包含Weibull类的实例,因此您不能将其与double相乘。

您说您生成了随机数,但没有生成。为此,请使用Sample()方法:

var theta = (maximumAngle - minimumAngle) * myweibull.Sample() + minimumAngle;

这将为您提供一个韦伯分布的随机数。如果您需要更多随机数,请任一反复呼叫Sample()

for( int i = 0; i < n; i++ )
{
    var theta = (maximumAngle - minimumAngle) * myweibull.Sample() + minimumAngle;
    ...
}

或使用Samples()

一次生成多个随机数
var randomNumbers = new double[n];
myweilbull.Samples(randomNumbers);
for( int i = 0; i < n; i++ )
{
    var theta = (maximumAngle - minimumAngle) * randomNumbers[i] + minimumAngle;
    ...
}

编辑更新的问题

对于您的问题,rtheta 必须是独立的,否则角度和半径将完全相关,并且所有生成的点都在一条线上。

for( int i = 0; i < n; i++ )
{
    var theta= (maximumAngle - minimumAngle) * myweibull.Sample() + minimumAngle;
    var r = maxRange * Math.Sqrt( myweibull.Sample() );

    var x = centreX + r * Math.Cos(theta);
    var y = centreY + r * Math.Sin(theta);

    // Do something with your generated point (x, y)
}

如果仅为thetar生成50个随机值,但仅计算一次xy,那么您将只有一个随机点。

但是我仍然想知道您要实现什么目标,因为这些点在行业中不会平均分布,而是在Weibull分布。