jared jared - 1 year ago 46
Javascript Question

when generating normally-distributed random values, what is the most efficient way to define the range?

FYI: random == pseudo-random

A. when generating uniformly-random numbers, I can specify a range, i.e.:

//generates numbers between -5 and 15

B. generating a set of random values with a version of Gaussian-esque normal randomness:

//pass in the mean and standard deviation
function randomNorm(mean, stdev) {
return Math.round((Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1))*stdev+mean);

//using the following values:
//results in a range of 397-403, or +-range of 3
//results in a range of 372-429, or +-range of 30
//results in a range of 326-471, or +-range of 75

each one gives me a range of approximately standard_deviation*(+-3) (assuming I left the program running longer).

C. I can calculate this range as follows:

  1. assuming I want a range from 300-500, so var total_range = 200;

  2. my mean is 400, my +-range is total_range/2 (var r = 100)

  3. so standard_deviation would be r/3 or in this case 33.333.

This seems to be working, but I have no idea what I'm doing with math so I feel like an idiot, this solution feels kludgy and not totally accurate.

My question:
is there some formula that I'm dancing around that can help me here? my requirements are as follows:

  1. must be able to define a range of numbers accurately.

  2. must be done in JavaScript, as efficiently as possible.

I think maybe I'm close but it's not quite there.

Answer Source

Subtracting two random numbers doesn't give you a normal distribution, it will give you numbers that decline linearly on both sides of zero. See the red diagram in this fiddle:


To get a good approximation of normal distribution, add six random numbers together. See the green diagram in the fiddle.

So, to get normally distributed random numbers, use:

((Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) - 3) / 3

This method is based on the central limit theorem, outlined as the second method here: http://en.wikipedia.org/wiki/Normal_distribution#Generating_values_from_normal_distribution