C#生成带数字和大小写字母的随机码
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
public class RandomPW { /// <summary> /// 生成单个随机数字 /// </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> /// 生成随机数 /// </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 类,使用指定的种子值。 // // 参数: // Seed: // 用来计算伪随机数序列起始值的数字。 如果指定的是负数,则使用其绝对值。 public Random(int Seed); public class RandomPW
{ /// <summary> /// 生成单个随机数字 /// </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> /// 生成随机数 /// </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 编辑过 |
关键字查询
相关文章
正在查询... |