javascript - generate random NOT in array -
this question has answer here:
- select random value not in array 7 answers
i trying generate random number not exist in restricted number array.
js
var restricted = [3, 4, 7];  function getrand () {      rand = math.floor(math.random() * 10);      if ($.inarray(rand, restricted) === -1) {         return rand;     } else {         try again     }   }  rand = getrand (); 
if problem how implement "try again" part, call getrand recursively:
var restricted = [3, 4, 7];  function getrand() {     var rand = math.floor(math.random() * 10);     if ($.inarray(rand, restricted) === -1) {         return rand;     } else {         return getrand();     } }  var rand = getrand(); also, if you're using jquery $.inarray method, suggest using  array.prototype.indexof instead (for incompatible browsers, shim available here).
Comments
Post a Comment