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