 # Fill an array with unique values

Hi all,
with this code

``````function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function getRandomInts(num) {
var ints = [];
for (var i = 0; i < num; i++) {
ints.push(getRandomInt(1, 91));
}
return ints;
}
``````

I build an array of random number
but how can I do to get it the same length and
with unique value.

I mean if now I run
getRandomInts(10)
I can get this
[77, 60, 9, 55, 21, 71, 85, 11, 77, 34] (77 is double)
but I want
[77, 60, 9, 55, 21, 71, 85, 11, n, 34] (n stand for just an other random number)

You could do it by turning the `for` loop into a `while` loop and then every time you draw a new random number, check if it is unique to the array. If not then discard it and keep going until the desired length has been reached:

``````function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function getRandomInts(num) {
var ints = [];
while (ints.length < num-1) {
var randNum = getRandomInt(1, 91);
if(!ints.indexOf(randNum) > -1){
ints.push(randNum);
}
}
return ints;
}
var arr = getRandomInts(10);
console.log(arr);``````

Thx Here’s an alternative approach that will achieve the same end result.

``````Array.prototype.shuffle = function() {
var r=[],c = this.slice(0);
while (c.length) r.push(c.splice(Math.random() * c.length, 1));
return r;
};
Array.prototype.populate = function(n) {
return Object.keys(Object(0+Array(n)));
};

var getRandomInts = function(num, min, max) {
var a = [].populate(max).slice(min);
a = a.shuffle();
return a.slice(0,num);
}

var arr = getRandomInts(10,1,91);
console.log(arr);
``````

Note that Pullo’s solution is far more efficient when you only want a small number of unique values out of the range but would become less efficient as the number of values you want increases.

If you want most or all of the values then my alternative would be more efficient.

Consider: `var arr = getRandomInts(90,1,91);` which needs to return all the numbers between 1 and 90 in random order where the test if it already exists in the output would need to keep generating values for the 90th entry until it figures out which one possible value hasn’t been added yet.

1 Like

wow thx a tons for sharing No problem. I already had the populate and shuffle code written and your question got me thinking about how they could be used to achieve the result.

It gave me an idea for an article about alternative approaches to solving this problem and how the one alternative gets progressively slower as you need more values whereas the alternative takes about the same time regardless of how many entries you want but is slower than the other for when you only need a small number of values.

Hello maybe be too late, but i have a question: i would achieve the same result, but with objects array.
So, i need unique object and not number and can be so usefull if you can help me to modify this condition

``````if(!ints.indexOf(randNum) > -1){
ints.push(randNum);
}
``````

to reflet objects unique value maybe by property…
Thanks a lot

Hi,

Would you mind starting a new topic in the JS forum with your question.

ok i do