javascript - generate random NOT in array -


this question has answer here:

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

Popular posts from this blog

Pull out data related to my apps from Android Play Store and iOS App Store -

Change php variable from jquery value using ajax (same page) -

How can I fetch data from a web server in an android application? -