2020-07-31

C#获取相邻字符不相等的随机字符串(0~9,A~Z,或自定义)

生成随机字符串

        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <param name="codeLen">数据的长度</param>
        /// <returns></returns>
        public string CreateRandomCode(int codeLen)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9,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";// 若想生成验证码则通常会去掉类似的字符,如:O与0、I与1
            string[] allCharAry = allChar.Split(',');
            string randomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeLen; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));// 保证随机性跟严谨
                }
                int t = rand.Next(35);// allChar数组索引最大值
                if (temp == t)
                {
                    return CreateRandomCode(codeLen);//防止随机字符串中两个相邻的字符相等
                }
                temp = t;
                randomCode += allCharAry[t];
            }
            return randomCode;
        }

C#获取相邻字符不相等的随机字符串(0~9,A~Z,或自定义)bsci平台入驻costco美国​闪拍电商平台Tophatter SFB玩法步骤,全品类佣金低至10%,销量暴涨3000%重磅!速卖通主战场遭遇挑战,亚马逊或收购俄罗斯平台OzonFacebook营销攻略(一)2017年深圳春节哪里有庙会?2017深圳元宵节去哪玩?元宵一样赏月去2017深圳国庆节有什么好去处?

No comments:

Post a Comment