title: What is the best way to generate a random key within PHP?
body: <p>I'm looking to create a reusable function that will generate a random key with printable ACSII characters of chosen length (anywhere from 2 to 1000+). I'm thinking printable ASCII characters would be 33-126. They key does not need to be completely unique, just unique if generated at the exact same millisecond (so <code>uniqid()</code> won't work).</p>
  
  <p>I'm thinking a combination of <code>chr()</code> and <code>mt_rand()</code> might work.</p>
  
  <p>Is this the way to go, or is something else the best method?</p>
  
  <p><strong>Edit:</strong> <code>uniqid()</code> will also not work because it doesn't have a length parameter, it's just whatever PHP gives you.</p>
  
  <p><strong>My Idea</strong>: This is what I came up with:</p>
  
  <pre><code>function GenerateKey($length = 16) {
      $key = '';
  
      for($i = 0; $i &lt; $length; $i ++) {
          $key .= chr(mt_rand(33, 126));
      }
  
      return $key;
  }
  </code></pre>
  
  <p>Are there any problems with this?</p>
  
  <p><strong>Another Edit:</strong> Most of the other questions deal with password generation. I want a wider variety of characters and I don't care about <code>1</code> vs <code>l</code>. I want the maximum number of possible keys to be possible.</p>
  
  <p><strong>Note: the generated key does not necessarily have to be cryptographically secure.</strong></p>