[Solved] jQuery: how to generate 3 different random integers given limits/range?


This will work:

var myArray = [];

for(var i = 0; i < 3; i++) {
    var numberIsInArray = false;
    var rand = generateRandomNumb(1, 21);
    for(var j = 0; j < myArray.length; j++){
        if(rand === myArray[j]) {
            numberIsInArray = true;
            i--;
        }
    }
    if(!numberIsInArray){
       myArray.push(rand);
    }
}

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function generateRandomNumb(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

You save 3 different random between 1 – 20 i myArray. Now you can do what you whant with the numbers in the array.

4

solved jQuery: how to generate 3 different random integers given limits/range?