mephisto’blog

二人行,也有吾师!
Fork me on GitHub

随机生成数

 public string RandomNum(int n)
    {
        string strchar = "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";
        string[] VcArray = strchar.Split(',');
        string VNum = "";       //由于字符串很短,就不用StringBuilder了  
        int temp = -1;           //记录上次随机数值,尽量避免产生几个一样的随机数  
        //采用一个简单的算法以保证生成随机数的不同  
        Random rand = new Random();
        for (int i = 1; i < n + 1; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(36);
            if (temp != -1 && temp == t)
            {
                return RandomNum(n);
            }
            temp = t;
            VNum += VcArray[t];
        }
        return VNum;//返回生成的随机数  
    } 

posted @ 2007-09-21 11:10  sinodzh  阅读(390)  评论(1编辑  收藏  举报