我的问题是这样的:我需要根据高斯/正态分布生成一个随机数,并创建一个宽度为0.1的直方图。
class Gaussian
{
public static double Next(Random r, double mu = 0, double sigma = 1)
{
var u1 = r.NextDouble();
var u2 = r.NextDouble();
var rand_std_normal = Math.Sqrt(-2.0 * Math.Log(u1)) *
Math.Sin(2.0 * Math.PI * u2);
var rand_normal = mu + sigma * rand_std_normal;
return rand_normal;
}
}
我正在使用上面的函数来生成高斯随机值。
现在,为了创建直方图,我需要一种能够自动将高斯值转换为数组索引的计算。类似于以下内容:
static void Main(string[] args)
{
const int N = 1000;
int time = N;
const double binsDistance = 0.1;
int binsCount = (int)(N * binsDistance);
Random rand = new Random();
int[] array = new int[binsCount];
int side = 0;
for (int i = 0; i < time; i++)
{
double gauss = Gaussian.Next(rand);
int binNo = Normalization.Normalize(0, binsCount - 1, gauss);
array[binNo]++;
}
}
为此,我尝试了两次计算。
- the first one is here.
- the second one is here.
第一个问题是,它不能正确处理负数。
第二个问题是,它生成了太多的零值。
因此,我有两个问题:
- #1和#2之间的基本区别是什么?
- 我如何才能实现自己的目标?