Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Generate random numbers with fixed mean and standard deviation

My question:

How can I create a list of random numbers with a given mean and standard deviation (sd) in Javascript?

Example:

I want to create a list with 5 random numbers in a range between 1 to 10. The resulting mean should be 5 and the standard deviation should be 2.

What I've done so far:

My idea was (http://jsfiddle.net/QASDG/3/):

  1. Make a function (named "createData") which creates 5 random numbers between 1 and 10 and push them into an "array"
  2. This function should also calculate the mean (and the sd) of those 5 numbers.
  3. With a "do-while"-loop: Do the above function as long as the "average becomes 5" (and "sd becomes 2"). But when I call the do-while Loop, the browser crashes of course because there are a lot of cases when the average is != 5 (and the sd != 2).

Note: I haven't add the SD yet, since the code isn't very elegant.

I've found another script from this site which stated: "The goal of this code is to generate a collection filled with 5 normally distributed random numbers with a mean of 1.0 and a standard deviation of 0.5." http://rosettacode.org/wiki/Random_numbers#JavaScript. I've tried to adjust it according to my needs by adding "Math.floor" and changed the condition in the "for"-loop (i < 5).

function randomNormal() {
    return Math.floor(Math.cos(2 * Math.PI * Math.random()) * Math.sqrt(-2 * Math.log(Math.random())))
}

var a = []
for (var i=0; i < 5; i++){
    a[i] = randomNormal() / 2 + 1
}

How I want to modify my code:

I'm not quite sure whether I've understood the mathematical part of this code. According to the webpage it will create a normal distribution (which is great but not necessary). So what I need in addition is:

  • Where can I change the given mean and sd (e.g. mean = 5, sd = 2) in order to create random numbers (which fulfill those properties).
  • How do I implement the range between 1 and 10? (Maybe I could use something like: Math.floor(Math.random()*10)+1)

Thanks a lot!

like image 222
OhDaeSu Avatar asked Mar 24 '14 20:03

OhDaeSu


People also ask

How do you generate random numbers with mean and standard deviation?

Use the formula "=NORMINV(RAND(),B2,C2)", where the RAND() function creates your probability, B2 provides your mean and C2 references your standard deviation. You can change B2 and C2 to reference different cells or enter the values into the formula itself.

How do you generate random numbers with mean and standard deviation in R?

Random numbers from a normal distribution can be generated using rnorm() function. We need to specify the number of samples to be generated. We can also specify the mean and standard deviation of the distribution. If not provided, the distribution defaults to 0 mean and 1 standard deviation.

How will you generate the random numbers in JavaScript?

Javascript creates pseudo-random numbers with the function Math. random() . This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.

How does Matlab generate random numbers with mean and standard deviation?

r = normrnd( mu , sigma ) generates a random number from the normal distribution with mean parameter mu and standard deviation parameter sigma . r = normrnd( mu , sigma , sz1,...,szN ) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension.

What is the random () method in JavaScript?

The random() method helps us generate random numbers, as the name implies. The Math.random() method returns a decimal number or floating-point, pseudo-random number between zero (inclusive) and one (exclusive). In mathematical terms, this is represented as 0 <= x < 1.

How to generate a random number with fixed length using JavaScript?

How to generate a random number with fixed length using JavaScript? To generate a fixed-length random number is similar to generating a random integer in a range. In this case, the range should be determined first based on the given length.

How to make random integers in JavaScript?

1 Math.random () Math.random () always returns a number lower than 1. 2 JavaScript Random Integers. Math.random () used with Math.floor () can be used to return random integers. 3 A Proper Random Function. As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer ...

How to return a random number between Min and Max in JavaScript?

This JavaScript function always returns a random number between min (included) and max (excluded): Example. function getRndInteger (min, max) {. return Math.floor(Math.random() * (max - min) ) + min; }.


Video Answer


1 Answers

How can I create a list of random numbers with a given mean and standard deviation (sd) in JavaScript?

This appears to be a question about randomly creating a list of numbers that has an exactly specified mean and an exactly specified standard deviation (and not a question about drawing random numbers from a specific probability distribution with a given mean and sd).

A straightforward solution is to draw a list of random numbers, then to shift and scale this list to have the desired mean and sd, as described in this answer from stats.stackexchange.

Say, we generate the following 5 random numbers between 1 and 10:

4.527991433628388
6.3254986488276055
5.123502737960912
7.3331068522336125
9.069573681037484

This list has a mean of 6.475934670737601 and sd of 1.8102412442104023.

Then, we transform each number in the list like this:

newNum = newSD * (oldNum - oldMean) / oldSD + newMean

By setting the new mean to 5 and new sd to 2, we get the following transformed list:

2.847863379160965
4.83379450402964
3.505799227476338
5.947025358346529
7.865517530986525

Computing the mean and sd of this list confirms that they are indeed 5 and 2.

Below is code demonstrating this approach in JavaScript:

// create a list of 5 random numbers between 1 and 10
var list = randomList(5, 1, 10);

// transform the list to have an exact mean of 5 and sd of 2
var newList = forceDescriptives(list, 5, 2);

// display the transformed list and descriptive statistics (mean and sd)
console.log('Transformed random list', newList, descriptives(newList));

// display the original list and descriptive statistics (mean and sd)
console.log('Original random list', list, descriptives(list));


/* demo functions */

function randomList(n, a, b) {
  // create a list of n numbers between a and b
  var list = [],
    i;
  for (i = 0; i < n; i++) {
    list[i] = Math.random() * (b - a) + a;
  }
  return list;
}

function descriptives(list) {
  // compute mean, sd and the interval range: [min, max]
  var mean,
    sd,
    i,
    len = list.length,
    sum,
    a = Infinity,
    b = -a;
  for (sum = i = 0; i < len; i++) {
    sum += list[i];
    a = Math.min(a, list[i]);
    b = Math.max(b, list[i]);
  }
  mean = sum / len;
  for (sum = i = 0; i < len; i++) {
    sum += (list[i] - mean) * (list[i] - mean);
  }
  sd = Math.sqrt(sum / (len - 1));
  return {
    mean: mean,
    sd: sd,
    range: [a, b]
  };
}

function forceDescriptives(list, mean, sd) {
  // transfom a list to have an exact mean and sd
  var oldDescriptives = descriptives(list),
    oldMean = oldDescriptives.mean,
    oldSD = oldDescriptives.sd,
    newList = [],
    len = list.length,
    i;
  for (i = 0; i < len; i++) {
    newList[i] = sd * (list[i] - oldMean) / oldSD + mean;
  }
  return newList;
}

Limitations

Note that, due to precision limits in floating-point arithmetic, the demo sometimes computes the exact mean and sd to deviate slightly (like an sd of 2.0000000000000004 instead of 2). Also note that, depending on the desired mean and sd, it may not be possible to generate a list of numbers within a desired range, as described in this answer on math.stackexchange. This means that the transformed list could be in a very different range than the original list. Also note that it is likely not possible to generate lists of random integers from a given range with a given integer mean and integer sd.

like image 102
Tomas Langkaas Avatar answered Sep 24 '22 13:09

Tomas Langkaas