java - 在Java中制作一个名称生成器,用于限制生成的字符数到 12,但不会截断字词?

  显示原文与译文双语对照的内容
0 0

我正在寻找一种制作一个随机 NAME 生成器的方法,这将限制生成字符串的字符数。

例如我有一个字符串"飞龙"和另一个字符串"killer13"。 如果随机匹配的话,有 14个字符。 如果我想将 2个完整字符串放入 12个字符,那么我不想得到"dragonkiller",因为"飞龙"和"killer13"的最后两个字符被匹配,因为它截断了"killer13"的最后两个字符。

时间: 原作者:

0 0

做这样的事情怎么样?


//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());

原作者:
0 0

你可以尝试以下方法:


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

无法部署这里代码,但必须正确。

我使用这个例子对随机生成器在一个区域中生成一个随机整数。

原作者:
0 0

这是我whipped的东西。 TMath.rand 只返回两个参数值之间的一个随机值。 对于真正的世界使用,必须有一个检查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 );
 }
}

原作者:
...