C#生成帶數(shù)字和大小寫字母的隨機碼
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
public class RandomPW { /// <summary> /// 生成單個隨機數(shù)字 /// </summary> public int CreateNum() { Random random = new Random((); int num = random.Next(10); return num; } /// <summary> /// 生成單個大寫隨機字母 /// </summary> public string CreateBigAbc() { //A-Z的 ASCII值為65-90 Random random = new Random(); int num = random.Next(65, 91); string abc = Convert.ToChar(num).ToString(); return abc; } /// <summary> /// 生成單個小寫隨機字母 /// </summary> public string CreateSmallAbc() { //a-z的 ASCII值為97-122 Random random = new Random(); int num = random.Next(97, 123); string abc = Convert.ToChar(num).ToString(); return abc; } /// <summary> /// 生成隨機數(shù) /// </summary> /// <returns></returns> public string GetRandomPW(int len) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { Random random = new Random(); switch (random.Next(3)) { case 0: sb.Append(CreateNum()); break; case 1: sb.Append(CreateSmallAbc()); break; case 2: sb.Append(CreateBigAbc()); break; } } var nowdata = DateTime.Now.ToString("yyyymmdd"); var result = nowdata + sb.ToString(); return result; } } // 摘要: // 新實例初始化 System.Random 類,使用依賴于時間的默認種子值。 public Random(); // // 摘要: // 新實例初始化 System.Random 類,使用指定的種子值。 // // 參數(shù): // Seed: // 用來計算偽隨機數(shù)序列起始值的數(shù)字。 如果指定的是負數(shù),則使用其絕對值。 public Random(int Seed); public class RandomPW
{ /// <summary> /// 生成單個隨機數(shù)字 /// </summary> public int CreateNum(int i) { Random random = new Random((int)DateTime.Now.Ticks+i); int num = random.Next(10); return num; } /// <summary> /// 生成單個大寫隨機字母 /// </summary> public string CreateBigAbc(int i) { //A-Z的 ASCII值為65-90 Random random = new Random((int)DateTime.Now.Ticks+i); int num = random.Next(65, 91); string abc = Convert.ToChar(num).ToString(); return abc; } /// <summary> /// 生成單個小寫隨機字母 /// </summary> public string CreateSmallAbc(int i) { //a-z的 ASCII值為97-122 Random random = new Random((int)DateTime.Now.Ticks+i); int num = random.Next(97, 123); string abc = Convert.ToChar(num).ToString(); return abc; } /// <summary> /// 生成隨機數(shù) /// </summary> /// <returns></returns> public string GetRandomPW(int len) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { Random random = new Random((int)DateTime.Now.Ticks+i); switch (random.Next(3)) { case 0: sb.Append(CreateNum(i)); break; case 1: sb.Append(CreateSmallAbc(i)); break; case 2: sb.Append(CreateBigAbc(i)); break; } } var result = sb.ToString(); return result; } } var newStr = new RandomPW(); textBox1.Text = newStr.GetRandomPW(6); 該文章在 2024/1/2 0:13:52 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |