代码改变世界

一段产生随机数的代码。

2006-10-16 12:56  Rainbow  阅读(601)  评论(0)    收藏  举报
代码如下:  
  private string createrandomcode(int codecount)
    {
        string allchar = "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";
        string[] allcharArray = allchar.Split(',');
        string randomcode = "";
        int temp = -1;
        System.Random rand = new Random();
        for (int i = 0; i < codecount; i++)
        {
            if (temp != -1)
                rand = new Random(i * temp * ((int)System.DateTime.Now.Ticks));
            int t = rand.Next(61);
            if (temp == t)
                return createrandomcode(codecount);
            temp = t;
            randomcode += allcharArray[t];
        }
        return randomcode;
    }

使用就直接运行函数就OK了啊。
如。Lable1.Text=createrandomcode(8). 8就是你想产生的随机码的位数啊。