在javascript中创建一个正常的分布式模式?

时间:2015-04-06 19:34:08

标签: javascript

我想通过添加随机性来创建看起来更像手绘线条的线条。我目前使用此公式来修改坐标

x-10+Math.floor(Math.random()*20)

这种随机分布是线性的,我想使用的东西更有可能击中目标。 X根据看起来像的东西,但不一定是钟形曲线。怎么办呢?

1 个答案:

答案 0 :(得分:1)

使用定义标准正态分布的概率密度函数:

function stdNormalDistribution (x) {
  return Math.pow(Math.E,-Math.pow(x,2)/2)/Math.sqrt(2*Math.PI);
}

该函数在0附近对称,这意味着stdNormalDistribution(x) == stdNormalDistribution(-x).

来源: Wikipedia: Normal Distribution

相关问题