在 Java 中制作一个名称生成器,将生成的字符数限制为 12 个,但不截断单词?
Making a name generator in Java that limits the amount of generated characters to 12, but doesn't cutt off words?
我正在寻找一种制作随机名称生成器的方法,它将生成的字符串的字符数限制为 12 个。
例如,我有一个字符串"Dragon"和另一个字符串"killer13"。如果它们随机匹配,则有 14 个字符。我希望它选择的名称能够将 2 个完整的字符串放入总共 12 个字符以下的名称中;所以如果 "Dragon" 和 "killer13" 匹配在一起,我不想得到 "Dragonkiller" 因为它切断了 "killer13".
的最后两个字符
做这样的事情怎么样?
// 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());
你可以试试这个:
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;
}
我无法部署此代码,但必须正确。
PD:我用这个例子来生成随机数How do I generate random integers within a specific range in Java?
这是我突然想到的东西。 TMath.rand 只是 returns 两个参数值之间的随机值。对于现实世界的使用,必须检查 maxlen 值是否不是太小,以至于生成器找不到合适的东西。然后它将永远循环。或者只是删除在调用中指定最大长度的功能。
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 );
}
}
我正在寻找一种制作随机名称生成器的方法,它将生成的字符串的字符数限制为 12 个。
例如,我有一个字符串"Dragon"和另一个字符串"killer13"。如果它们随机匹配,则有 14 个字符。我希望它选择的名称能够将 2 个完整的字符串放入总共 12 个字符以下的名称中;所以如果 "Dragon" 和 "killer13" 匹配在一起,我不想得到 "Dragonkiller" 因为它切断了 "killer13".
的最后两个字符做这样的事情怎么样?
// 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());
你可以试试这个:
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;
}
我无法部署此代码,但必须正确。
PD:我用这个例子来生成随机数How do I generate random integers within a specific range in Java?
这是我突然想到的东西。 TMath.rand 只是 returns 两个参数值之间的随机值。对于现实世界的使用,必须检查 maxlen 值是否不是太小,以至于生成器找不到合适的东西。然后它将永远循环。或者只是删除在调用中指定最大长度的功能。
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 );
}
}