You are invited to Log in or Register a free Frihost Account!

# JAVA - random name generator

clydejaw
Have you guys ever heard of the alphabet frequency table?

it's just like 0.0341 = a etc (not correct, but just an example)

Anyway, that's part of the point.

I need to make a random name generator. And each random number that it produces, it tries to match with the closest alphabet decimal number...

Anyone got ne ideas that I cud start off with! I have a few but I wanna see if there is a more efficient way of doing it.
snowboardalliance
probably a double[] with the "weight" of each letter in alphabetical order. Then like a[(int)"A"-65] should be the first index and so on.

Now I would then use a method like this
http://www.weberdev.com/get_example-4543.html
to pick the random letters. It's in php but the idea is the same.
clydejaw
cheers.

I actually found out that you have to write a big method like:

anotherRandNum = 5;
int rand = (int)(Math.random()...etc
String name ="";

for(int i = 0; i<anotherRandNum; i++){
if (rand == 0.12702){
name += "e";
}
}

something quite similar to that, except with the whole alphabet! and that's gonna take a bloody long time. IT's either that way, or I'll have to create an array and use recursive calls etc. : /!!!!!
Indi
 clydejaw wrote: cheers. I actually found out that you have to write a big method like: anotherRandNum = 5; int rand = (int)(Math.random()...etc String name =""; for(int i = 0; i

Whoa. >_<

No, you don't need a big-ass switch statement (or a bunch of if statements) and you don't need recursion.

i don't understand what you're doing, but it sounds like something like this:
 Code: public class Test {    static private class CharacterFrequency    {       public float frequency;       public char character;             public CharacterFrequency(char c, float f)       {          character = c;          frequency = f;       }    }        static private CharacterFrequency[] frequencies;        static    {       frequencies = new CharacterFrequency[]       {          new CharacterFrequency('a', 0.8f),          new CharacterFrequency('c', 0.4f),          new CharacterFrequency('g', 0.2f),          new CharacterFrequency('t', 0.6f)          // ...          // Add as many as you need       };    }        private static char getRandomCharacter()    {       float v = (float)Math.random();             char c = frequencies[0].character;       float f = frequencies[0].frequency;             for (CharacterFrequency cf : frequencies)       {          if (v < cf.frequency && cf.frequency < f)          {             c = cf.character;             f = cf.frequency;          }       }             return c;    }        public static void main(String[] args)    {       int desired_length = 5;             StringBuffer buf = new StringBuffer(desired_length);             for (int i = 0; i < desired_length; ++i)       {          buf.append(getRandomCharacter());       }             System.out.println(buf.toString());    } }

That's just slapped together, and i haven't even really checked it, but it seems to work:
 Code: >java Test caaag >java Test cagcg >java Test aaaaa >java Test agaga >java Test taaca >java Test aagta

That's just with 4 letters. You can add as many as you want, along with frequencies, or change the way the random variable determines the frequency (i used cumulative frequencies) and so on.
clydejaw
oh my god.

you just totally kicked my ass in java.

I normally/ and tend to make things more difficult. I use recursion when i'm lazy. I'm not quite familiar with float, but it seems good enough for me. It's just the little part of my assignment . Mine works, but it's less efficient and we all know that efficient is best

cheers man
akalili
hi..i need to develop a java class that will calculate a statistical test like frequency test and serial test.
so, i dont have any idea how to build it. the frequency test is a test that will be count how many '0' and how many '1' in a certain file. so, its good if any body can help me.thanks.
leontius
I made this during my spare time a long time ago: http://leapon.net/en/random-name-generator-javascript

It's javascript, not java, but I hope you can at least read it. Full source code is found on the link above.

 Code: function getName(minlength, maxlength, prefix, suffix) {    prefix = prefix || '';    suffix = suffix || '';    //these weird character sets are intended to cope with the nature of English (e.g. char 'x' pops up less frequently than char 's')    //note: 'h' appears as consonants and vocals    var vocals = 'aeiouyh' + 'aeiou' + 'aeiou';    var cons = 'bcdfghjklmnpqrstvwxz' + 'bcdfgjklmnprstvw' + 'bcdfgjklmnprst';    var allchars = vocals + cons;    //minlength += prefix.length;    //maxlength -= suffix.length;    var length = rnd(minlength, maxlength) - prefix.length - suffix.length;    if (length < 1) length = 1;    //alert(minlength + ' ' + maxlength + ' ' + length);    var consnum = 0;    //alert(prefix);    /*if ((prefix.length > 1) && (cons.indexOf(prefix[0]) != -1) && (cons.indexOf(prefix[1]) != -1)) {       //alert('a');       consnum = 2;    }*/    if (prefix.length > 0) {       for (var i = 0; i < prefix.length; i++) {          if (consnum == 2) consnum = 0;          if (cons.indexOf(prefix[i]) != -1) {             consnum++;          }       }    }    else {       consnum = 1;    }        var name = prefix;        for (var i = 0; i < length; i++)    {       //if we have used 2 consonants, the next char must be vocal.       if (consnum == 2)       {          touse = vocals;          consnum = 0;       }       else touse = allchars;       //pick a random character from the set we are goin to use.       c = touse.charAt(rnd(0, touse.length - 1));       name = name + c;       if (cons.indexOf(c) != -1) consnum++;    }    name = name.charAt(0).toUpperCase() + name.substring(1, name.length) + suffix;    return name; }