Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select random value NOT in array

How would I select a random value (0 to 30) that is not in this array?

var list = new Array(1,3,4,7,8,9);
like image 999
Sparkup Avatar asked Dec 17 '22 11:12

Sparkup


1 Answers

Build the complementary array and pick random values from it.

var list2 = new Array();
for(var i=0; i<30; i++)
  if(!list.contains(i))
    list2.push(i);

Then:

var rand = list2[Math.floor(Math.random() * list2.length)];
like image 63
slaphappy Avatar answered Jan 03 '23 15:01

slaphappy