First, we use the fisherYates implementation (credit goes to @ChristopheD) and extends the prototype array to have the shuffle function available
function arrayShuffle () { var i = this.length, j, temp; if ( i === 0 ) return false; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i]; this[i] = this[j]; this[j] = temp; } } Array.prototype.shuffle =arrayShuffle; var numbers = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); numbers.shuffle();
Now, using the pop method, we get the number from our seed until it is empty.
numbers.pop();
To make sure we have an array filled with numbers in the start and end range, we use a simple loop to create our seed.
var start = 1; var end = 5; var numbers = new Array(); for (var i = start; i <= end; i++) { numbers.push(i); }
here is an example on jsfiddle
UPDATE: add fisheryates algo for more efficient use
source share