~ 1 min read

Javascript - Generate Pseudo Random NON REPEATING Array of Numbers

I have recently had to have a random non repeating numbers. So I used these 2 methods to first generate the set and then shuffle it.

The example will generate numbers 1..99 in a random order.

function generateNumArr(limit) {
    ret = [];
    for (var i = 1; i < limit; i++) {
        ret.push(i);
    }

    return ret;
}


function shuffle(array) {
    var i = array.length,
        j = 0,
        temp;

    while (i--) {

        j = Math.floor(Math.random() * (i+1));

        // swap randomly chosen element with current element
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    return array;
}


var ranNums = shuffle(generateNumArr(100));

Please let me know in the comments what you think about this method.