title: Generating non-repeating random numbers in JS
body: <p>I have the following function</p>
  
  <pre><code>function randomNum(max, used){
   newNum = Math.floor(Math.random() * max + 1);
  
    if($.inArray(newNum, used) === -1){
     console.log(newNum + " is not in array");
     return newNum;
  
    }else{
     return randomNum(max,used);
    }
  }
  </code></pre>
  
  <p>Basically I am creating a random number between 1 - 10 and checking to see if that number has already been created, by adding it to an array and checking the new created number against it. I call it by adding it to a variable..</p>
  
  <pre><code>UPDATED:
  for(var i=0;i &lt; 10;i++){
  
     randNum = randomNum(10, usedNums);
     usedNums.push(randNum);
  
     //do something with ranNum
  }
  </code></pre>
  
  <p>This works, but in Chrome I get the following error:</p>
  
  <pre><code>Uncaught RangeError: Maximum call stack size exceeded
  </code></pre>
  
  <p>Which I guess it's because I am calling the function inside itself too many times. Which means my code is no good.</p>
  
  <p>Can someone help me with the logic? what's a best way to make sure my numbers are not repeating?</p>