///具体代码 CaptChaClick Desc:Test; Author:Vance; Date:2023-08-01 17:00:00;
/// <summary>
/// create a random key
/// </summary>
static readonly Random Random = new Random(~unchecked((int)DateTime.Now.Ticks));
static readonly char[] NumberList = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
static readonly char[] CharList = { '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' };
static readonly char[] MixedList = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; //remove I & O
#region 生成随机数字
/// <summary>
/// 生成随机数字
/// </summary>
/// <param name="Length">生成长度</param>
public static string Number(int Length)
{
return Create(Length, false, NumberList);
}
#endregion
#region 生成随机字母与数字
/// <summary>
/// 生成随机字母与数字
/// </summary>
/// <param name="Length">生成长度</param>
public static string Mixed(int Length)
{
return Create(Length, false, MixedList);
}
#endregion
#region 生成随机纯字母随机数
/// <summary>
/// 生成随机纯字母随机数
/// </summary>
/// <param name="Length">生成长度</param>
public static string Char(int Length)
{
return Create(Length, false, CharList);
}
#endregion
/// <summary>
/// Create the CAPTCHA specified Length, Sleep and List.
/// </summary>
/// <returns>The create.</returns>
/// <param name="Length">Length.</param>
/// <param name="Sleep">If set to <c>true</c> sleep.</param>
/// <param name="List">List create CAPTCHA based on</param>
private static string Create(int Length, bool Sleep, char[] List)
{
if (Sleep) Thread.Sleep(3);
char[] Pattern = List;
string result = string.Empty;
int n = Pattern.Length;
for (int i = 0; i < Length; i++)
{
int rnd = Random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
///调用
string CaptChaReturnMessAge = string.Empty;
CaptChaReturnMessAge += "\n";
CaptChaReturnMessAge += "生成随机数字【" + Number(10) + "】" + "\n";
CaptChaReturnMessAge += "生成随机字母与数字【" + Mixed(10) + "】" + "\n";
CaptChaReturnMessAge += "生成随机纯字母随机数【" + Char(10) + "】" + "\n";
CaptChaReturnMessAge += "\n";
MessageBox.Show(CaptChaReturnMessAge);
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交