c#固定长度的随机字符串例子
程序员文章站
2023-12-13 11:22:40
复制代码 代码如下:/// /// 固定长度的随机字符串/// ///
/// <summary>
/// 固定长度的随机字符串
/// </summary>
/// <param name="length">长度</param>
/// <returns>随机串</returns>
public static string getrandstringex(int length)
{
char[] charlist = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[] rev = new char[length];
random f = new random();
for (int i = 0; i < length; i++)
{
rev[i] = charlist[math.abs(f.next(127)) % length];
}
return new string(rev);
}
复制代码 代码如下:
/// <summary>
/// 固定长度的随机字符串
/// </summary>
/// <param name="length">长度</param>
/// <returns>随机串</returns>
public static string getrandstringex(int length)
{
char[] charlist = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[] rev = new char[length];
random f = new random();
for (int i = 0; i < length; i++)
{
rev[i] = charlist[math.abs(f.next(127)) % length];
}
return new string(rev);
}