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

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -