title: Generate many random numbers
body: <p>I need to generate 4 million unique random numbers between 1 and 1 billion of 9 digits. Fill with zeros until 9 digits.</p>
  
  <p>My script works (but slow) for eq. 400000 numbers. But not for 4 millions</p>
  
  <p>I need the numbers in a text file. Its fine to just CTRL+S the output.</p>
  
  <p>Is there any ways to optimize the memory/performance?</p>
  
  <pre><code>function zeroPad(num, places) {
    var zero = places - num.toString().length + 1;
    return Array(+(zero &gt; 0 &amp;&amp; zero)).join("0") + num;
  }
  var arr = []
  while (arr.length &lt; 4000000) {
    var randomnumber = Math.ceil(Math.random() * 100000000)
    if (arr.indexOf(randomnumber) &gt; -1) continue;
    arr[arr.length] = randomnumber;
  }
  for (i = 0; i &lt; arr.length; i++) {
    document.write(zeroPad(arr[i], 9) + '&lt;br /&gt;');
  }
  </code></pre>