Himmators
Himmators

Reputation: 15026

Create a normal distribution-looking pattern in javascript?

I want to create lines that look more like hand-drawn lines by adding randomness to them. I currently use this formula to modify the coordinates

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

This random distribution is linear, I would like to use something that makes it more likely to hit the target. X according to something that looks like, but doesn't have to be a bell-curve. How can this be done?

Upvotes: 2

Views: 5337

Answers (1)

Aleuck
Aleuck

Reputation: 294

Use the probability density function that defines the standard normal distribution:

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

The function is symmetric around 0, which means stdNormalDistribution(x) == stdNormalDistribution(-x).

Source: Wikipedia: Normal Distribution

Upvotes: 7

Related Questions