title: How to efficiently randomly select array item without repeats?
body: <p>I'm aware that this question is around in many guises but I have not been able to find an answer relating to my specific issue of efficiency.</p>
  
  <p>I have the below code that works just fine. </p>
  
  <p>I have a 10 item array from which I randomly select an item (on enter key press). The code keeps an array of the 5 most recent choices which cannot be randomly selected (to avoid too much repetition over time).</p>
  
  <p>If the chooseName() function initially selects a name that has been used in the recent 5 goes it simply breaks and calls itself again, repeating until it finds a "unique" name.</p>
  
  <p>I have two questions:</p>
  
  <ol>
  <li><p>Is it correct to say this is a "recursive function"?</p></li>
  <li><p>I am worried that theoretically this could keep looping for a long time before finding a unique name - is there a more efficient way to do this?</p></li>
  </ol>
  
  <p>Thank you for any help.</p>
  
  <pre><code>    var a = ["Roger", "Russell", "Clyde", "Egbert", "Clare", "Bobbie", "Simon", "Elizabeth", "Ted", "Caroline"];
      var b = [];
  
      var chooseName = function () {
      var unique = true;
      b.length = 5;
      num = Math.floor(Math.random() * a.length);
      name = a[num];    
          for (i = 0; i &lt; a.length; i++) {
          if (b[i] == name) {
              chooseName();
              unique = false;
              break;
              }
          }
          if (unique == true) {
          alert(name);
          b.unshift(name);
          }
      }
  
  
      window.addEventListener("keypress", function (e) {
          var keycode = e.keyCode;
          if (keycode == 13) {
          chooseName();
          }
      }, false);
  </code></pre>