随机生成包含大小写和数字的字符串

写了一个随机产生字符串的方法,字符串由大小字母和数字组成,

我这里利用的是GUID 作为源点,避免重复出现

  /// <summary>
        ///  
        /// </summary>
        /// <param name="leng">长度</param>
        public static void GenerateCheckCode(int leng)
        {
            var num2 = Guid.NewGuid().ToString();
            while(num2.Length<leng-1){num2 += Guid.NewGuid().ToString();}

            int max = new Random().Next(100000, 999999);
            char[] list = new char[leng];
            int[] array = new int[leng] ;

            for (int i = 0; i < leng; i++){array[i] = i;}

            int[] temp;
            temp = new int[array.Length];
            for (int i = 0; i < temp.Length; i++) { temp[i] = array[i]; }
            Random rand = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < temp.Length; i++)
            {
                int x, y; int t;
                x = rand.Next(0, temp.Length);
                do{y = rand.Next(0, temp.Length);} while (y == x);
                t = temp[x];
                temp[x] = temp[y];
                temp[y] = t;
            }
            for (int i = 0; i < leng; i++)
            {
                if (i %3== 0)
                {
                    list[temp[i]] = (char)(0x30 + ((ushort)(num2[i] * max % 10)));
                }else if (i % 3 == 1)
                {
                    list[temp[i]] = (char)(0x41 + ((ushort)(num2[i] * max % 0x1a)));
                }else if (i % 3 == 2)
                {
                    list[temp[i]] = (char)(0x61 + ((ushort)(num2[i] * max % 0x1a)));
                }
            }
            
            Console.WriteLine(string.Join("", list));
        }

 

转载于:https://www.cnblogs.com/lsgControl/p/9234206.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值