Skip to content

Instantly share code, notes, and snippets.

@le717
Created November 14, 2014 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save le717/93ae7c13cdb69751ea25 to your computer and use it in GitHub Desktop.
Save le717/93ae7c13cdb69751ea25 to your computer and use it in GitHub Desktop.
For my blog post "6 billion names" (http://wp.me/p1V5ge-1KI)
/**
* 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