Get Random Str - CSharp System

CSharp examples for System:Random

Description

Get Random Str

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;//from   w  w  w  .j a v a 2 s  .c  o  m

public class Main{
        public static string GetRandomStr(int strLength, int randomSeed)
        {
            string[] strArray = "1,2,3,4,5,6,7,8,9,0,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,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".Split(new char[] { ',' });
            Random random = null;
            if (randomSeed > 0)
            {
                random = new Random(randomSeed);
            }
            else
            {
                random = new Random();
            }
            string str2 = "";
            while (str2.Length < strLength)
            {
                str2 = str2 + strArray[random.Next(strArray.Length)];
            }
            return str2;
        }
        public static string GetRandomStr(int strLength)
        {
            return GetRandomStr(strLength, 0);
        }
}

Related Tutorials