If I understand correctly, are you just looking for a permutation (i.e. numbers randomized without repeats) of numbers 1-10? Maybe try creating a randomized list of these numbers, once, at the beginning, and then just work your way through them?
This will compute a random permutation of numbers in nums :
var nums = [1,2,3,4,5,6,7,8,9,10], ranNums = [], i = nums.length, j = 0; while (i--) { j = Math.floor(Math.random() * (i+1)); ranNums.push(nums[j]); nums.splice(j,1); }
So, for example, if you were looking for random numbers from 1 to 20 that were even even, you could use:
nums = [2,4,6,8,10,12,14,16,18,20];
Then just read ranNums to call random numbers.
This does not risk that he will increasingly look for unused numbers, as you found in your approach.
EDIT : after reading this and running the jsperf test, it seems like a much better way to do this is to switch the Fisher-Yates Shuffle:
function shuffle(array) { var i = array.length, j = 0, temp; while (i--) { j = Math.floor(Math.random() * (i+1));
In principle, this is more efficient, avoiding the use of “expensive” array operations.
BONUS EDIT . Another possibility is to use generators (assuming support ):
function* shuffle(array) { var i = array.length; while (i--) { yield array.splice(Math.floor(Math.random() * (i+1)), 1)[0]; } }
Then to use:
var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10]); ranNums.next().value; // first random number from array ranNums.next().value; // second random number from array ranNums.next().value; // etc.
where ranNums.next().value will eventually be evaluated to undefined after you go through all the elements in the shuffled array.
In general, it will not be as effective as Fisher-Yates Shuffle, because you are still splice -in the array. But the difference is that you only do this work now when you need it, and don’t do it all in advance, so depending on your use case, it might be better.