protected string yzm()
    {
        string[] dzm ={ "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[] xzm ={ "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[] num ={ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        string str = "";
            for (int j = 0; j < 3; j++)
            {
                Random r = new Random();
                int i = r.Next(1, 3);                if (i == 1)
                {
                    Random r1 = new Random();
                    int R1 = r1.Next(1, 26);
                    str += dzm[R1];                }
                if (i == 2)
                {
                    Random r2 = new Random();
                    int R2 = r2.Next(1, 26);
                    str += xzm[R2];
                }            }
       return str;
    }
为什么循环输出的内容都相同啊,我该怎么改?

解决方案 »

  1.   

    使用:
    Random r = new Random(i*(int)DateTime.Now.Ticks);
      

  2.   


    以下是我设置断点调试出的结果:sFLrwHJpu
      

  3.   

    1、你要把所有字符放在一个数组。string[] dzm;
    2、                  //随机的字符串
                           string code = "";
                //需要的随机字符个数
                int codeLen=0;
                Random rand = new Random();
                for (int i = 0; i < codeLen; i++)
                {
                    code += strArr[rand.Next(0, dzm.Length)];
                }
      

  4.   

    string[] dzm = { "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[] xzm = { "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[] num = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            string str = "";
            for (int j = 0; j < 3; j++)
            {
                Random r = new Random();
                int i = r.Next(1, 3);            if (i == 1)
                {
                    Random r1 = new Random();
                    int R1 = r1.Next(1, 26);
                    str += dzm[R1];
                    System.Threading.Thread.Sleep(10);            }
                if (i == 2)
                {
                    Random r2 = new Random();
                    int R2 = r2.Next(1, 26);
                    str += xzm[R2];
                    System.Threading.Thread.Sleep(10);            }
                        }
            this.tbCount.Text = str;
      

  5.   

    忘记说明了,我是在上面代码中加上了System.Threading.Thread.Sleep(10);
      

  6.   


    在上面代码中加上了System.Threading.Thread.Sleep(10);
    这个问题就解决啦!