C#生成随机字符串
|
admin
2021年1月28日 12:19
本文热度 2779
|
public class RandomStringBuilder
{
private int createNum()
{
Random random = new Random(Guid.NewGuid().GetHashCode());
int num = random.Next(10);
return num;
}
private string createBigAbc()
{
Random random = new Random(Guid.NewGuid().GetHashCode());
int num = random.Next(65, 91);
string abc = Convert.ToChar(num).ToString();
return abc;
}
private string createSmallAbc()
{
Random random = new Random(Guid.NewGuid().GetHashCode());
int num = random.Next(97, 123);
string abc = Convert.ToChar(num).ToString();
return abc;
}
public string Create(int length)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
Random random = new Random(Guid.NewGuid().GetHashCode());
switch (random.Next(3))
{
case 0:
sb.Append(createNum());
break;
case 1:
sb.Append(createSmallAbc());
break;
case 2:
sb.Append(createBigAbc());
break;
}
}
return sb.ToString();
}
}
该文章在 2021/1/28 12:20:08 编辑过