Last active
April 11, 2016 07:42
-
-
Save Hyra/fd2a01edb3d65dcaee2096cf61c722a9 to your computer and use it in GitHub Desktop.
Geneate n amount of unique codes containing the characters you specify and write them to a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generate X unique codes and write them to a file | |
// npm install crypto | |
// node generate_codes.js | |
var crypto = require('crypto'); | |
var fs = require('fs'); | |
function random (howMany, chars) { | |
chars = chars || "ACDEFGHJKMNPQRTWXYZ234679"; | |
var rnd = crypto.randomBytes(howMany), | |
value = new Array(howMany), | |
len = chars.length; | |
for (var i = 0; i < howMany; i++) { | |
value[i] = chars[rnd[i] % len] | |
}; | |
return value.join(''); | |
} | |
var codefile = fs.openSync('./codes.txt', 'w'); | |
var codes = []; | |
var code = ''; | |
while(codes.length < 25000) { | |
code = random(6); | |
if(codes.indexOf(code) < 0) { | |
// console.log(code); | |
codes.push(code); | |
fs.write(codefile, code + '\n'); | |
} else { | |
console.log('dup'); | |
} | |
} | |
fs.closeSync(codefile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment