Created
November 14, 2014 20:02
-
-
Save le717/93ae7c13cdb69751ea25 to your computer and use it in GitHub Desktop.
For my blog post "6 billion names" (http://wp.me/p1V5ge-1KI)
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
/** | |
* Randomly scrambles the given username. | |
* https://gist.github.com/le717/4c2188ca814f56575a26. | |
* @param {String} realName The username to be scrambled. | |
* @returns {String} The scrambled username. | |
*/ | |
function scrambleName(realName) { | |
"use strict"; | |
var newName = "", | |
usedChar = []; | |
function randomLetter(myString) { | |
return Math.floor(Math.random() * myString.length); | |
} | |
for (var i = 0; i < realName.length; i++) { | |
var letterIndex = randomLetter(realName); | |
// That letter has already been used | |
while (usedChar.indexOf(letterIndex) > -1) { | |
letterIndex = randomLetter(realName); | |
} | |
usedChar.push(letterIndex); | |
newName += realName.charAt(letterIndex); | |
} | |
return newName; | |
} | |
/** | |
* Calculate the factorial of a number. | |
* http://stackoverflow.com/a/4438167. | |
* @param {Number} x | |
* @returns {Number} | |
*/ | |
function fact(x) { | |
"use strict"; | |
if (x === 0) { | |
return 1; | |
} | |
return x * fact(x - 1); | |
} | |
var realName = "jimbobjeffers", | |
wanted = {}, | |
combos = fact(realName.length); | |
for (var i = 0; i < combos; i++) { | |
var name = scrambleName(realName); | |
// Progress indicator | |
if (i % 100 === 0) { | |
console.log(i); | |
} | |
// We are looking for a name with "Bobjim" anywhere in it | |
if (name.indexOf("bobjim") > -1) { | |
wanted[i] = name; | |
} | |
} | |
// Report the results. | |
if (Object.keys(wanted).length === 0) { | |
console.log("Bobjim was not found."); | |
} else { | |
console.log(wanted); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment