samedi 8 juillet 2017

Generate a random number that does guarantees a mean different to 0

I have been using randoms in order to get a number between two numbers, but if I execute this operation N times and get the average of numbers, I always find that the average is the same.

I want to have a random number, that guarantees that the average is going to be random also. Is this possible?

This is my example:

function getRandom(from, to) {
    var rangeSize = to - from;
    return parseInt(Math.random() * rangeSize + from);
};

var sum = 0;
var i;
var n = 99999;
for (i = 0; i < n; i++) {
    sum += getRandom(100, 200);
}
console.log(sum / n);

In that case, whenever you execute that code, you will find that the average printed is 150.. which is the number between 100 and 200..

How can I make this code have a different average?




Aucun commentaire:

Enregistrer un commentaire