Skip to content

Instantly share code, notes, and snippets.

@altbdoor
Created December 3, 2013 03:10
Show Gist options
  • Save altbdoor/7763299 to your computer and use it in GitHub Desktop.
Save altbdoor/7763299 to your computer and use it in GitHub Desktop.
function getCodes (charset, codeLength, codeCount) {
var codeList = [],
codeListLength = 0,
charsetLength = charset.split('').length,
codePerCharset = charsetLength / codeLength,
collision = 0,
i, temp;
while (codeListLength < codeCount) {
var charsetCopy = charset.slice(0).split('');
for (i=charsetLength - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
temp = charsetCopy[i];
charsetCopy[i] = charsetCopy[j];
charsetCopy[j] = temp;
}
charsetCopy = charsetCopy.join('');
for (i=0; i<codePerCharset && codeListLength<codeCount; i++) {
temp = charsetCopy.substr((i * codeLength), codeLength);
if (codeList.indexOf(temp) == -1) {
codeList.push(temp) && codeListLength++;
}
else {
collision++;
}
}
}
console.log(collision);
return codeList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment