Change . The best solution that this or others posted here can be found in this answer to this question when asked to answer in 2008. To summarize: generate an array (as Darin suggests in his answer below) and shuffle it using Knuth-Yates- Fisher shuffle . Do not use a naive shuffle, use one that is known to have good results.
This is pretty much how I would do it, yes. I would probably use an object to track the integers that I already had, as it is convenient. For instance:.
var ints = {};
Then, as soon as you have created a new random number, check it and possibly save it:
if (!ints[number]) {
ints[number] = true;
results.push(number);
}
source
share