1. 程式人生 > 實用技巧 >生成隨機數 純數字,純字母,數字+字母

生成隨機數 純數字,純字母,數字+字母

/// <summary>
/// 生成隨機純字母隨機數
/// </summary>
/// <param name="IntStr">生成長度</param>
/// <returns></returns>
public static string Str_char(int Length)
{
    return Str_char(Length, false);
}
/// <summary>
/// 生成隨機純字母隨機數
/// </summary>
/// <param name="Length">生成長度</param>
/// <param name="Sleep">
是否要在生成前將當前執行緒阻止以避免重複</param> /// <returns></returns> public static string Str_char(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(3); char[] Pattern = new char[] { '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' }; string result = ""; int n = Pattern.Length; System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) { int rnd = random.Next(0, n); result += Pattern[rnd]; }
return result; } /// <summary> /// 生成隨機純字母隨機數 /// </summary> /// <param name="Length">生成長度</param> /// <param name="Sleep">是否要在生成前將當前執行緒阻止以避免重複</param> /// <returns></returns> public static string Str_charSamll(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(3); char[] Pattern = new char[] { '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' }; string result = ""; int n = Pattern.Length; System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) { int rnd = random.Next(0, n); result += Pattern[rnd]; } return result; }