title: JavaScript pseudo-random sequence generator
body: <p>I need to generate a deterministic (i.e. repeatable) sequence of <a href="http://xkcd.com/221/" rel="nofollow">pseudo-random</a> numbers given an initial seed and select the nth item from that sequence.</p>
  
  <p>If JavaScript's random function was seedable, I could just do:</p>
  
  <pre><code>function randomNth(seed, seq)
  {
      var r;
      Math.randomSeed(seed);
      for (var i = 0; i++ &lt; seq; i++)
      {
          r = Math.random();
      }
      return r;
  }
  </code></pre>
  
  <p>However, it's not, and alternative, seedable PRNGs look to be a little slow; asking for the 250th number would be expensive.</p>
  
  <p>I think a hash is what I want here, perhaps something like <code>md5(seed + seq) % max</code> but JavaScript doesn't have md5() and if I'm doing it in code there's probably a better choice of hash.</p>
  
  <p>I'd like a function where</p>
  
  <p><code>x = randomNth(seed, seq, maxVal)  // x is int &amp;&amp; x &gt;= 0 &amp;&amp; x &lt; maxVal</code></p>
  
  <p>or, ideally</p>
  
  <p><code>x = randomNth(seed, seq)  // x &gt;= 0 &amp;&amp; x &lt; 1, same as Math.random()</code></p>
  
  <p>Other requirements:</p>
  
  <ul>
  <li>must run in node.js and in a browser</li>
  <li>numbers should be statistically random (or close enough as the period will be small)</li>
  <li>should be O(1) and reasonably performant</li>
  </ul>