C# 如何制作长度为5的随机数字和字母字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9995839/
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
How to make random string of numbers and letters with a length of 5?
提问by Ian Lundberg
Possible Duplicate:
Is this a good way to generate a string of random characters?
How can I generate random 8 character, alphanumeric strings in C#?
This is the code that I have so far.
这是我到目前为止的代码。
private void button1_Click(object sender, EventArgs e)
{
string rand1 = RandomString(5);
string rand2 = RandomString(5);
string rand3 = RandomString(5);
string rand4 = RandomString(5);
string rand5 = RandomString(5);
textBox1.Text = rand1 + "-" + rand2 + "-" + rand3 + "-" + rand4 + "-" + rand5;
}
private static Random random = new Random((int)DateTime.Now.Ticks);
private string RandomString(int Size)
{
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < Size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
BUT it just creates a random string of 5 chars. I want it to create a string of 5 chars and integers. How would I do this? Thanks in advance!
但它只是创建一个 5 个字符的随机字符串。我希望它创建一个由 5 个字符和整数组成的字符串。我该怎么做?提前致谢!
采纳答案by BrokenGlass
Use an input array to draw your values from:
使用输入数组从以下位置绘制值:
private static string RandomString(int length)
{
const string pool = "abcdefghijklmnopqrstuvwxyz0123456789";
var builder = new StringBuilder();
for (var i = 0; i < length; i++)
{
var c = pool[random.Next(0, pool.Length)];
builder.Append(c);
}
return builder.ToString();
}
Or the (inevitable) Linq solution:
或者(不可避免的)Linq 解决方案:
private static string RandomString(int length)
{
const string pool = "abcdefghijklmnopqrstuvwxyz0123456789";
var chars = Enumerable.Range(0, length)
.Select(x => pool[random.Next(0, pool.Length)]);
return new string(chars.ToArray());
}
回答by CodesInChaos
replace
代替
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
by
经过
string chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ch=chars[random.Next(chars.Length)];
Note that your code doesn't create unpredictable random strings. In particular there are only 2 billion possible results, and if your computer gets restarted often, some of them are much more likely than others.
请注意,您的代码不会创建不可预测的随机字符串。特别是只有 20 亿个可能的结果,如果您的计算机经常重新启动,其中一些结果的可能性要大得多。
If you want unpredictable random strings, you should use RNGCryptoServiceProvider. You can fine an example at https://stackoverflow.com/a/1344255/445517, you just need to add the hyphens.
如果你想要不可预测的随机字符串,你应该使用RNGCryptoServiceProvider。你可以在https://stackoverflow.com/a/1344255/445517 上做一个例子,你只需要添加连字符。
回答by Ohad Schneider
Copying from jon skeet's answer... https://stackoverflow.com/a/976674/67824
从 jon skeet 的回答中复制... https://stackoverflow.com/a/976674/67824
Random rand = new Random();
public const string Alphabet =
"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public string GenerateString(int size)
{
char[] chars = new char[size];
for (int i=0; i < size; i++)
{
chars[i] = Alphabet[rand.Next(Alphabet.Length)];
}
return new string(chars);
}

