title: Random Binary in JavaScript/Node.JS
body: <p>I am trying to find the fastest way of generating a random binary string of a certain length. As in, a certain number of random booleans.</p>
  
  <p>Here's my current code - is there a much faster way?</p>
  
  <pre><code>function getRandomBits(n){
      var generator = crypto.pseudoRandomBytes;
      var generated = "";
      while(generated.length &lt; n){
          var randomBytes = generator(4).readUInt32BE(0, true).toString(2);
          //Remove first byte as this is always one and so not random
          randomBytes = randomBytes.substring(1, randomBytes.length - 1);
          if(n - generated.length &gt; randomBytes.length) generated = generated + randomBytes;
          else generated = generated + randomBytes.substring(0, n - generated.length);
      }
      console.log(generated);
      return generated;
  }
  </code></pre>
  
  <p>Thanks!</p>