Pages

Men

rh

7/30/2014

How to get Random String in C#

  public String GetRandomNumberString(int iLength)
        {
            String strRandomString;
           
            int iZero, iNine, iA, iZ, iCount = 0, iRandNum;

            string sRandomString;

            Random rRandom = new Random(System.DateTime.Now.Millisecond);

            iZero = (int)'0';

            iNine = (int)'9';

            iA = (int)'A';

            iZ = (int)'Z';

            sRandomString = string.Empty;

            while (iCount < iLength)
            {
                iRandNum = rRandom.Next(iZero, iZ);

                if (((iRandNum >= iZero) && (iRandNum <= iNine) || (iRandNum >= iA) && (iRandNum <= iZ)))
                {

                    sRandomString = sRandomString + Convert.ToChar(iRandNum);

                    iCount = iCount + 1;

                }
            }

            strRandomString = sRandomString.ToString();

            return strRandomString;

        }

    }

No comments :

Post a Comment