Created
October 30, 2012 16:26
-
-
Save Afforess/3981313 to your computer and use it in GitHub Desktop.
Random name generator
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
import java.util.Random; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println(getRandomName(new Random(), 10)); | |
} | |
//Returns vowels based on the most common letters and the frequency that they occur | |
public static char getRandomVowel(Random rand) { | |
int r = rand.nextInt(38100); | |
if (r < 8167) return 'a'; | |
if (r < 20869) return 'e'; | |
if (r < 27835) return 'i'; | |
if (r < 35342) return 'o'; | |
return 'u'; | |
} | |
public static char getRandomConsonant(Random rand) { | |
int r = rand.nextInt(34550); | |
r += rand.nextInt(34550); | |
if (r < 1492) return 'b'; | |
if (r < 4274) return 'c'; | |
if (r < 8527) return 'd'; | |
if (r < 10755) return 'f'; | |
if (r < 12770) return 'g'; | |
if (r < 18864) return 'h'; | |
if (r < 19017) return 'j'; | |
if (r < 19789) return 'k'; | |
if (r < 23814) return 'l'; | |
if (r < 26220) return 'm'; | |
if (r < 32969) return 'n'; | |
if (r < 34898) return 'p'; | |
if (r < 34993) return 'q'; | |
if (r < 40980) return 'r'; | |
if (r < 47307) return 's'; | |
if (r < 56363) return 't'; | |
if (r < 57341) return 'v'; | |
if (r < 59701) return 'w'; | |
if (r < 59851) return 'x'; | |
if (r < 61825) return 'y'; | |
return 'z'; | |
} | |
public static String generateRandomWord(Random rand, int maxLength) { | |
StringBuilder builder = new StringBuilder(); | |
char nextLetter; | |
int length = Math.max(4, rand.nextInt(maxLength)); | |
for (int i = 0; i < length; i++) { | |
int r = rand.nextInt(1000); | |
if (r < 381) { | |
nextLetter = getRandomVowel(rand); | |
} else { | |
nextLetter = getRandomConsonant(rand); | |
} | |
if (i == 0) { | |
nextLetter = Character.toUpperCase(nextLetter); | |
} | |
builder.append(nextLetter); | |
} | |
return builder.toString(); | |
} | |
public static boolean isValidName(String name) { | |
int consonantCount = 0; | |
int vowelCount = 0; | |
int vowelStreak = 0; | |
int consonantStreak = 0; | |
name = name.toLowerCase(); | |
for (int i = 0; i < name.length(); i++) { | |
char ch = name.charAt(i); | |
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { | |
vowelCount++; | |
vowelStreak++; | |
consonantStreak = 0; | |
} else { | |
consonantCount++; | |
consonantStreak++; | |
vowelStreak = 0; | |
} | |
if (consonantStreak > 3 || vowelStreak > 4) { | |
return false; | |
} | |
} | |
//More than 75% of the word is vowels | |
if ((vowelCount * 100 / Math.max(1, vowelCount + consonantCount)) >= 75) { | |
return false; | |
} | |
//More than 70% of the word is consonants | |
if ((consonantCount * 100 / Math.max(1, vowelCount + consonantCount)) >= 70) { | |
return false; | |
} | |
return true; | |
} | |
public static String getRandomName(Random rand, int maxLength) { | |
String randomName = ""; | |
int tries = 3; | |
maxLength = Math.max(4, maxLength); | |
while(tries > 0) { | |
String name = generateRandomWord(rand, maxLength); | |
if (isValidName(name)) { | |
//first word is always valid | |
if (randomName.length() == 0) { | |
randomName = name; | |
} else { | |
int newLength = randomName.length() + name.length(); | |
if (newLength <= maxLength) { | |
randomName += " " + name; | |
} else { | |
tries--; | |
} | |
} | |
} | |
} | |
return randomName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment