Skip to content

Instantly share code, notes, and snippets.

@Hyra
Last active April 11, 2016 07:42
Show Gist options
  • Save Hyra/fd2a01edb3d65dcaee2096cf61c722a9 to your computer and use it in GitHub Desktop.
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
// 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