1. 程式人生 > >隨機生成不重複字串

隨機生成不重複字串

 // 一:隨機生成不重複數字字串  
        private int rep = 0;
        public string GenerateCheckCodeNum(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;   

            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        }

//方法二:隨機生成字串(數字和字母混和) 
        public string GenerateCheckCode(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {

 char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                } str = str + ch.ToString();
            } return str;
        }

// 從字串裡隨機得到,規定個數的字串. 

 private string GetRandomCode(string allChar, int CodeCount)
        {
            //string allChar = "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";  
            string[] allCharArray = allChar.Split(',');
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();

 for (int i = 0; i < CodeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                } int t = rand.Next(allCharArray.Length - 1);
                while (temp == t)

                {
                    t = rand.Next(allCharArray.Length - 1);
                } temp = t;
                RandomCode += allCharArray[t];
            }
            return RandomCode;
        }

本人試過其實還是會生成重複的字串的,只是在各別的伺服器上會生成重複的。解決辦法如下:

 private void newPreCode()
        {
            try
            {
                BLL.Chc_PreCode bllCode = new BLL.Chc_PreCode();
                Model.Chc_PreCode modelCode = new Model.Chc_PreCode();

                string randStr0 = "";
                int preCode = 100;
                int count = 0;
                for (int i = 0; i < preCode; i++)
                {

                 if (count >= 100)//保持能生成100個不同的預碼
                    {
                        break;
                    }
                    RandomOperate randomOp = new RandomOperate();
                    string randStr1 = randomOp.GenerateCheckCode(12);//生成長度為12的字串
                    if (randStr0 != randStr1)//避免重複
                    {
                        randStr0 = randStr1;
                    }

                    else
                    {
                        preCode++;
                        continue;
                    }
                    modelCode.KeyId = Guid.NewGuid().ToString().ToLower();
                    modelCode.PreCode = randStr0;
                    modelCode.UserState = 0;//0未使用 1已被使用
                    modelCode.CreateTime = DateTime.Now;
                    bllCode.Add(modelCode);
                    count++;

                 }
                this.result = "{\"success\":1,\"msg\":\"success\"}";

              }
            catch (Exception ex)
            {
                this.result = "{\"success\":0,\"msg\":\"failed\"}";
            }
        }