Today i needed a name generator so i wrote one myself (i didn’t like the existing ones i found via Google. Here is the test code i used:
1: NameGenerator gen = new NameGenerator();
2: for (int i = 0; i < 30; i++) { System.out.println(gen.getName()); }
It produces names like:
Pausyp
Braurjnn
Thushoob
Einnjk
Qeidegh
Aub
Xoihut
Voopych
Gljint
Och
Stontaigh
Loofautt
Grennoogh
Jshjnt
Zjlouv
Fabeih
Aishof
Hoir
Ait
Jihoint
Aiv
Poim
Flott
Ustjv
Zjis
Xittoutt
I also added constructors for complete customization of the generate process. Here is the code:
1: public class NameGenerator {
2: private List vocals = new ArrayList();
3: private List startConsonants = new ArrayList();
4: private List endConsonants = new ArrayList();
5: private List nameInstructions = new ArrayList();
6: public NameGenerator() {
7: String demoVocals[] = { "a", "e", "i", "o", "u", "ei", "ai", "ou", "j",
8: "ji", "y", "oi", "au", "oo" };
9: String demoStartConsonants[] = { "b", "c", "d", "f", "g", "h", "k",
10: "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z",
11: "ch", "bl", "br", "fl", "gl", "gr", "kl", "pr", "st", "sh",
12: "th" };
13: String demoEndConsonants[] = { "b", "d", "f", "g", "h", "k", "l", "m",
14: "n", "p", "r", "s", "t", "v", "w", "z", "ch", "gh", "nn", "st",
15: "sh", "th", "tt", "ss", "pf", "nt" };
16: String nameInstructions[] = { "vd", "cvdvd", "cvd", "vdvd" };
17: this.vocals.addAll(Arrays.asList(demoVocals));
18: this.startConsonants.addAll(Arrays.asList(demoStartConsonants));
19: this.endConsonants.addAll(Arrays.asList(demoEndConsonants));
20: this.nameInstructions.addAll(Arrays.asList(nameInstructions));
21: }
22: /**
23: *
24: * The names will look like this
25: * (v=vocal,c=startConsonsonant,d=endConsonants): vd, cvdvd, cvd, vdvd
26: *
27: * @param vocals
28: * pass something like {"a","e","ou",..}
29: * @param startConsonants
30: * pass something like {"s","f","kl",..}
31: * @param endConsonants
32: * pass something like {"th","sh","f",..}
33: */
34: public NameGenerator(String[] vocals, String[] startConsonants,
35: String[] endConsonants) {
36: this.vocals.addAll(Arrays.asList(vocals));
37: this.startConsonants.addAll(Arrays.asList(startConsonants));
38: this.endConsonants.addAll(Arrays.asList(endConsonants));
39: }
40: /**
41: * see {@link NameGenerator#NameGenerator(String[], String[], String[])}
42: *
43: * @param vocals
44: * @param startConsonants
45: * @param endConsonants
46: * @param nameInstructions
47: * Use only the following letters:
48: * (v=vocal,c=startConsonsonant,d=endConsonants)! Pass something
49: * like {"vd", "cvdvd", "cvd", "vdvd"}
50: */
51: public NameGenerator(String[] vocals, String[] startConsonants,
52: String[] endConsonants, String[] nameInstructions) {
53: this(vocals, startConsonants, endConsonants);
54: this.nameInstructions.addAll(Arrays.asList(nameInstructions));
55: }
56: public String getName() {
57: return firstCharUppercase(getNameByInstructions(getRandomElementFrom(nameInstructions)));
58: }
59: private int randomInt(int min, int max) {
60: return (int) (min + (Math.random() * (max + 1 - min)));
61: }
62: private String getNameByInstructions(String nameInstructions) {
63: String name = "";
64: int l = nameInstructions.length();
65: for (int i = 0; i < l; i++) { char x = nameInstructions.charAt(0); switch (x) { case 'v': name += getRandomElementFrom(vocals); break; case 'c': name += getRandomElementFrom(startConsonants); break; case 'd': name += getRandomElementFrom(endConsonants); break; } nameInstructions = nameInstructions.substring(1); } return name; } private String firstCharUppercase(String name) { return Character.toString(name.charAt(0)).toUpperCase() + name.substring(1); } private String getRandomElementFrom(List v) {
66: return v.get(randomInt(0, v.size() - 1));
67: }
68: }
Comment Please.