title: Fastest way to generate a lot of unique small random numbers in JavaScript
body: <p>Wondering how to quickly generate lots of unique, small random numbers. When I implemented it like this it slows down exponentially it seems like, to the point where it never finishes, or will take hours to complete. Probably because it creates tons of duplicates toward the end.</p>
  
  <pre><code>var intsmap = {}
  var intsarray = []
  var i = 100000
  while (i--) {
    var int = randominteger(6)
    if (intsmap[int]) i++
    else {
      intsmap[int] = true
      intsarray.push(int)
    }
  }
  // return intsarray
  
  function randominteger(exp) {
    var string = rand(exp)
    return pad(string, exp)
  }
  
  function pad(num, size) {
    var s = rand(9) + num
    return s.substr(s.length - size)
  }
  
  function rand(exp) {
    var integer = Math.random() * Math.pow(10, exp) &lt;&lt; 0
    var string = toString(integer, '0123456789')
    return string
  }
  
  function toString(value, code) {
    var digit
    var radix = code.length
    var result = ''
  
    do {
      digit = value % radix
      result = code[digit] + result
      value = Math.floor(value / radix)
    } while (value)
  
    return result
  }
  </code></pre>
  
  <p>Wondering how to accomplish that but the code works within a few seconds if possible.</p>
  
  <p><strong>Update</strong></p>
  
  <ul>
  <li>I would like for the set of numbers to be distributed evenly over an arbitrary range (in this example 1000000 strings, not necessarily from 0-1000000, eg maybe 5050000 is in there).</li>
  <li>I would like for the numbers to not necessarily be valid numbers, just a string of integers. So for example they can include <code>01010101</code> as a valid string, even though that's not a valid number.</li>
  </ul>