1

I am looking for a way to make a random name generator, that will limit the amount of characters the generated string will have to 12.

For example, I have a String "Dragon" and another String "killer13". That has 14 characters if they get matched randomly. I want it to choose names that will fit 2 full Strings into a name under 12 characters total; so I don't want to get "Dragonkiller" if "Dragon" and "killer13" are matched together because it cut off the last two characters of "killer13".

David
  • 179
  • 1
  • 1
  • 10
  • Will you have all the name segments already figured out ahead of time? – Ascalonian Feb 13 '15 at 17:03
  • Yes, it will have something like this: String[] names1 = { "name", "name2", }; String[] names2 = { "string", "string2" }; And pick one from each array, and match them together edit: sorry for the atrocious formatting I am new here – David Feb 13 '15 at 17:05

3 Answers3

1

What about doing something like this?

// A List to hold all the names
List<String> namesList = new ArrayList<>();

// Create the full list of names
String[] names = {"mike", "Dragon", "jason", "freddy", "john", "mic"};

// Store them into the List
namesList = new ArrayList(Arrays.asList(names));

// Randomly get the first part of the name
int randomIndex = new Random().nextInt(names.length - 1);
String firstName = namesList.get(randomIndex);
String lastName = null;

// Figure out the size remaining
int remainSize = 12 - firstName.length();

// If desired, remove the element from the List so you don't get "DragonDragon"
namesList.remove(randomIndex);

// Randomly shuffle the list
long seed = System.nanoTime();
Collections.shuffle(namesList, new Random(seed));

// For each name, grab the first one that will complete the size 12
for (String name : namesList) {
    int nameSize = name.length();

    if (nameSize <= remainSize) {
        lastName = name;
        break;
    }
}

String newName = firstName + lastName;
System.out.println("Generated name: " + newName + ", with size " + newName.length());
Ascalonian
  • 14,409
  • 18
  • 71
  • 103
  • Wow!! Thanks so much! I actually learned so much from this one answer thanks to your comments as well. Thanks again this is exactly what I needed. – David Feb 13 '15 at 18:50
  • Glad it worked! :-) i figured giving you just the code wouldn't help you out in the future. It's important to know *why* the code is done that way. – Ascalonian Feb 13 '15 at 18:51
  • @David - I updated the `Collections.shuffle()` method to make it more random – Ascalonian Feb 13 '15 at 18:57
  • I agree 100%. The best way to learn is by doing it yourself. (at least for me) haha – David Feb 13 '15 at 22:56
0

You can try some how this:

String[] names1 = { "name", "name2", "name3", "name4" };
String[] names2 = { "name", "name2", "name3", "name4" };
int lengthNames1 = names1.Length();
int lengthNames2 = names2.Length();
String name1Random = names1[randInt(0,lengthNames1)-1];
String name2Random = names2[randInt(0,lengthNames2)-1];
String finalName = ( name1Random + name1Random ).substring(0, 11);

public static int randInt(int min, int max) {

    // NOTE: Usually this should be a field rather than a method
    // variable so that it is not re-seeded every call.
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

I can't deploy this code but must be correct.

PD: I use this example to random generator How do I generate random integers within a specific range in Java?

Community
  • 1
  • 1
Kevin F
  • 169
  • 7
  • 17
0

Here's something I whipped up. The TMath.rand just returns a random value between the two argument values. For real-world use there would have to be a check that the maxlen value is not so small the generator can't find anything that fits. Then it would loop forever. Or just remove the ability to specify the max length in the call.

public class NameGenerator{
    private final String[] Names1 = { "Dragon", "Duck", "Pig", "Phoenix", "Behemoth" };
    private final String[] Names2 = { "Slayer", "Brusher", "Companion", "Rider", "Food" };
    private final int Name1Len = Names1.length;
    private final int Name2Len = Names2.length;
    private int mMaxNameLen;

    public NameGenerator( int MaxNameLen ){
        mMaxNameLen = MaxNameLen;
    }

    public String getName(){
        return getName( mMaxNameLen );
    }

    public String getName( int maxlen ){
        String lFirst = Names1[ (int) TMath.rand( 0, mName1Len - 1 ) ];
        String lSecond = Names2[ (int) TMath.rand( 0, mName2Len - 1 ) ];

        while( lFirst.length() + lSecond.length() > maxlen ){
            System.err.println( "Rejected: " + lFirst + lSecond );
            lFirst = Names1[ (int) TMath.rand( 0, mName1Len - 1 ) ];
            lSecond = Names2[ (int) TMath.rand( 0, mName2Len - 1 ) ];
        }
        return lFirst + lSecond;
    }

    private void run( int trys ){
        for( int i = 0; i < trys; i++ ){
            System.out.println( "Accepted: " + getName() );
        }
    }

    public static void main( String[] args ){
        new NameGenerator( 12 ).run( 15 );
    }
}
TommCatt
  • 5,498
  • 1
  • 13
  • 20
  • Wow! Thanks. I'm using a name generator to create artificially intelligent players in my MMO, and they have a maximum name space of 12, so this will be very helpful generating their names. :) – David Feb 14 '15 at 02:08