2

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?

Himmators
  • 14,278
  • 36
  • 132
  • 223
  • 1
    https://github.com/errcw/gaussian – Dai Apr 06 '15 at 19:35
  • You can define a bell curve as an array of points (for example) and randomly pick from that array. But that is a bit brute force, [are you looking for something like this?](http://stackoverflow.com/questions/25582882/javascript-math-random-normal-distribution-gaussian-bell-curve) – Spencer Wieczorek Apr 06 '15 at 19:43
  • Many alternatives there, most are complex or voted down because they are incorrect. I only need something that looks a bit bell-curve-ish, it's not to specific. Does anyone have a sugestion? – Himmators Apr 06 '15 at 20:21
  • 1
    Relevant: http://stackoverflow.com/q/29325069/251311 – zerkms Apr 06 '15 at 20:25

1 Answers1

7

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

Aleuck
  • 294
  • 1
  • 12