字母数字混合无重复函数

public string ran2()
        {
            string s = "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[] sz = s.Split(',');
            System.Collections.ArrayList al = new ArrayList();
            for (int i = 0; i < 10; i++)
            {
                if (al.Count == 0)
                {
                    System.Random rd = new Random();
                    int a = rd.Next(0, 35);
                    al.Add(a);
                }
                else
                {
                    int c = 0;
                    bool isonly = true;

                    while (isonly)
                    {
                        System.Random rd = new Random();
                        int b = rd.Next(0, 25);
                        if (this.isone(al, b))
                        {
                            isonly = false;
                            c = b;
                        }
                    }
                    al.Add(c);
                }
            }
            string rand = "";
            for (int j = 0; j < al.Count; j++)
            {

                rand += sz[Int32.Parse(al[j].ToString())];

            }
            return rand;
        }
        //下面还有一个判断唯一的函数  
        private bool isone(System.Collections.ArrayList a, int b)
        {
            bool one = true;
            for (int i = 0; i < a.Count; i++)
            {
                if (a[i].ToString() == b.ToString())
                {
                    one = false;
                    i = a.Count;
                }
            }
            return one;
        }  

 

 

调用:

 

for (int i = 0; i < 200;i++ )
            {
                Response.Write(ran2()+"<br/>");
                Response.Write("=====================================================<br/>");
            }

posted on 2009-01-22 14:16  草原和大树  阅读(308)  评论(0编辑  收藏  举报