admin管理员组

文章数量:1403452

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?

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?

Share Improve this question edited Apr 6, 2015 at 20:21 Himmators asked Apr 6, 2015 at 19:34 HimmatorsHimmators 15.1k39 gold badges136 silver badges233 bronze badges 4
  • 1 github./errcw/gaussian – Dai Commented Apr 6, 2015 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? – Spencer Wieczorek Commented Apr 6, 2015 at 19:43
  • Many alternatives there, most are plex 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 Commented Apr 6, 2015 at 20:21
  • 1 Relevant: stackoverflow./q/29325069/251311 – zerkms Commented Apr 6, 2015 at 20:25
Add a ment  | 

1 Answer 1

Reset to default 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

本文标签: Create a normal distributionlooking pattern in javascriptStack Overflow