C#中的随机名称生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14687658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random name generator in c#
提问by CSharpDev4Evr
I have a list of female and male first names then a list of last names in arrays.
我有一个女性和男性名字的列表,然后是数组中的姓氏列表。
What I was trying to do was use the random generator to take those names in those arrays and output a random first name and last name depending on what I call.
我试图做的是使用随机生成器在这些数组中获取这些名字,并根据我的称呼输出一个随机的名字和姓氏。
After I finish that I was going to reference that method in other classes instead of having to write it out each individual time.
完成后,我将在其他类中引用该方法,而不必每次都将其写出来。
Here is the code I have so far:
这是我到目前为止的代码:
private void RandName()
{
string[] maleNames = new string[1000] { "aaron", "abdul", "abe", "abel", "abraham", "adam", "adan", "adolfo", "adolph", "adrian"};
string[] femaleNames = new string[1000] { "abby", "abigail", "adele", "adrian"};
string[] lastNames = new string[1000] { "abbott", "acosta", "adams", "adkins", "aguilar"};
Random rand = new Random(DateTime.Now.Second);
if (rand.Next(1, 2) == 1)
{
FirstName = maleNames[rand.Next(0, maleNames.Length - 1)];
}
else
{
FirstName = femaleNames[rand.Next(0, femaleNames.Length - 1)];
}
}
My Question is:How to create a random name generator using the arrays of names I created?
我的问题是:如何使用我创建的名称数组创建随机名称生成器?
采纳答案by Chepene
I guess the result of your random generator is the same every time..
我猜你的随机生成器的结果每次都是一样的..
If this is your problem, you shouldn't create local variable rand
every time. Create field instead.
如果这是您的问题,则不应rand
每次都创建局部变量。改为创建字段。
Smth like:
喜欢:
private static Random rand = new Random(DateTime.Now.Second);
private void RandName()
{
...
}
回答by RB.
Ok, without knowing the problem you are facing, this is a bit of a stab in the dark, but I think your problem is that you've declared the array size as 1000
, but only entered around 10 values in each one. Therefore, your array is only 1% full, so you will mostly obtain empty names.
好的,在不知道您面临的问题的情况下,这有点暗中刺伤,但我认为您的问题是您已将数组大小声明为1000
,但每个值只输入了大约 10 个值。因此,您的数组只有 1% 满,因此您将主要获得空名称。
Try the following:
请尝试以下操作:
// Note I have not specified the array size.
string[] maleNames = {"aaron", "abdul", "abe", "abel", "abraham", "adam", "adan", "adolfo", "adolph", "adrian"};
string[] femaleNames = {"abby", "abigail", "adele", "adrian"};
string[] lastNames = {"abbott", "acosta", "adams", "adkins", "aguilar"};
回答by Tim Schmelter
Since the random instance is created in RandName
you will create the same names when the method is called very fast(f.e. in a loop). You should use a field or pass the random as argument to the method.
由于随机实例是在其中RandName
创建的,因此当该方法被非常快地调用时(循环中的fe),您将创建相同的名称。您应该使用一个字段或将随机数作为参数传递给该方法。
Apart from that, you will never get to the else
block here
除此之外,你永远不会到达else
这里的街区
if (rand.Next(1, 2) == 1)
since the second parameter of Random.Next
is the exclusive upper bound and it always generates 1. So you might want this instead:
因为的第二个参数Random.Next
是唯一的上限,它总是生成 1。所以你可能想要这个:
if (rand.Next(0, 2) == 1)
回答by Oliver
I think you should take a look at AutoPoco.
我认为你应该看看AutoPoco。
One exampleon how to use it.
关于如何使用它的一个示例。
And if you take a deeper lookinto the documentation you'll find an already existing data source for first and last names (which can be used, enlarged or replaced).
如果您更深入地查看文档,您会发现名字和姓氏的现有数据源(可以使用、放大或替换)。
回答by Kaz Duston McGown
Format you strings like so...
像这样格式化你的字符串......
string[] maleNames = new string[] { "aaron", "abdul", "abe", "abel", "abraham", "adam", "adan", "adolfo", "adolph", "adrian"};
string[] femaleNames = new string[] { "abby", "abigail", "adele", "adrian"};
string[] lastNames = new string[] { "abbott", "acosta", "adams", "adkins", "aguilar"};
Or For me the easier way is to use a List. I use them for iterating through files. The main differences are you have to add them one at a time using List.Add() and it uses List.Count instead of Array.Length. List also allow you to modify entries without having to resize the array. In your case the list you're trying to create would look like this
或者对我来说更简单的方法是使用列表。我使用它们来遍历文件。主要区别在于您必须使用 List.Add() 一次添加一个,并且它使用 List.Count 而不是 Array.Length。List 还允许您修改条目而无需调整数组大小。在您的情况下,您尝试创建的列表如下所示
List<string> maleNames = new List<string>();
List<string> femaleNames = new List<string>();
List<string> lastNames = new List<string>();
then use System.IO to open separate files and read each line in to the three list. It's just the way that I'd do it.
然后使用 System.IO 打开单独的文件并将每一行读入三个列表。这就是我要做的方式。
回答by HuckFin.7b
public string GenerateToken(Byte length) {
var bytes = new byte[length];
var rnd = new Random();
rnd.NextBytes(bytes);
return Convert.ToBase64String(bytes).Replace("=", "").Replace("+", "").Replace("/", "");
}
Is a very simple generator ..
是一个非常简单的生成器..
回答by Waqar Khan
public static string GenerateName(int len)
{
Random r = new Random();
string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "l", "n", "p", "q", "r", "s", "sh", "zh", "t", "v", "w", "x" };
string[] vowels = { "a", "e", "i", "o", "u", "ae", "y" };
string Name = "";
Name += consonants[r.Next(consonants.Length)].ToUpper();
Name += vowels[r.Next(vowels.Length)];
int b = 2; //b tells how many times a new letter has been added. It's 2 right now because the first two letters are already in the name.
while (b < len)
{
Name += consonants[r.Next(consonants.Length)];
b++;
Name += vowels[r.Next(vowels.Length)];
b++;
}
return Name;
}