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 );
}
}