asp.net随机数生成总是重复。如下:
结果:
8EBWP5KK
8EBWP5KK
8EBWP5KK
8EBWP5KK
8EBWP5KK
8EBWP5KK
8EBWP5KK
8EBWP5KK
8EBWP5KK
8EBWP5KK
代码:
 class ka_action
    {
        public static string get_pass(int num)
        {
            string[] source = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            string code = "";
            Random rd = new Random();
            for (int i = 0; i < num; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }
        
    }
private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10;i++ )
            {
                textBox5.Text += ka_action.get_pass(8) + "\r\n";
              
            }
            
            MessageBox.Show("ok");
     
        }

解决方案 »

  1.   


      for (int i = 0; i < num; i++)
      {
    Random rd = new Random();
      code += source[rd.Next(0, source.Length)];
      }
      

  2.   

    Random rd = new Random();
    丢到循环里面去。
      

  3.   

    Random rnd = new Random(DateTime.Now.Millisecond); 
    循环中
      

  4.   


       string code = "";
                for (int i = 0; i < num; i++)
                {
                    string[] source = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };                Random rd = new Random();
                  
                        code += source[rd.Next(0, source.Length)]+"\r\n";
                  
                }

    这样也不行
      

  5.   


    能不能用string[] source = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };这里边的作为抽取的字段?
      

  6.   

    Console.WriteLine(get_pass(5));
                Console.WriteLine(DateTime.Now.Ticks.ToString() + "\r\n");
                Console.WriteLine(get_pass(5));
                Console.WriteLine(DateTime.Now.Ticks.ToString() + "\r\n");
                Console.WriteLine(get_pass(5));
                Console.WriteLine(DateTime.Now.Ticks.ToString() + "\r\n");
                Console.WriteLine(get_pass(5));
                Console.WriteLine(DateTime.Now.Ticks.ToString() + "\r\n");
                Console.ReadKey();
    用这个测试的结果是   他的Ticks 也就是刻度是一样的  运行速度太快造成的
    你要解决这个问题  最好是在
    get_pass
    方法一进来的时候加一句
    Thread.Sleep(100);我加了以后 问题解决,,,主要是随即值 不是真的随机 他是按刻度时间计算的一个串
      

  7.   

    嗯刚才试了下 快速生成 不影响速度情况下   好像最低可以设置 Sleep(20); 就可以不重复了
      

  8.   

    由于生成时间太短,所以会造成这样的结果!
    int arryValue;
    Random randrom = new Random(unchecked((int)DateTime.Now.Ticks));
    arryValue = randrom.Next(minValue, maxValue);
    上面的代码可以解决!
      

  9.   

    你的代码不可以解决,,,刻度不变 你int 一百次他也是原来的数!
    他所生成的随即串 也都是同一串!!
      

  10.   

    for (int i = 0; i < 5; i++)
     {
      code += source[rd.Next(source.Length-1)] ;
       System.Threading.Thread.Sleep(10);
    }没有时间结果 瞬间生成的结果都是一样
      

  11.   

    string[] source =
    这句的上面 加上一句Thread.Sleep(20);就可以了
      

  12.   

    ArrayList pass = new ArrayList();
        int i = 0;
        while (i < 10)
        {
          String s = get_pass(8);
          if (!pass.Contains(s))
          {
            pass.Add(s);
            i++;
          }
        }
        for (i = 0; i < 10; i++)
        {
          textBox5.Text += pass[i].ToString() + "\r\n";    }
      

  13.   

    Random rd = new Random();  这个要放到循环里边啊   不让你每次循环生成的随机数都是一样的   
      

  14.   

      
      static Random rd = new Random();  public static string get_pass(int num)
      {
      string[] source = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
      string code = "";
      
      for (int i = 0; i < num; i++)
      {
      code += source[rd.Next(0, source.Length)];
      }
      return code;
      }
        
      }
      

  15.   

     private static char[] constant ={ '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', '1', '2', '3', '4', '5', '6', '7', '8', '9' };/// <summary>
        /// 随机取数
        /// </summary>
        /// <param name="Length">你要取的随机数的位数</param>
        /// <returns></returns>
        public static string GenerateRandom(int Length) {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(constant.Length);
            Random rd = new Random();
            for (int i = 0; i < Length; i++) { newRandom.Append(constant[rd.Next(constant.Length - 1)]); }
            return newRandom.ToString();
        }
      

  16.   

    把Random rd = new Random()拿到get_pass(int num)这个方法外面去定义
      

  17.   

    线程的问题 在循环里加上Thread.sleep(1000)就行啦
      

  18.   

    MSDN上是这样写的:
        使用 Random 类生成随机数时生成是从种子值开始。如果反复使用同一个种子,就会生成相同的数字系列。由于时钟分辨率有限,频繁地创建不同的 Random 对象会创建出产生相同随机数序列的随机数生成器。通过创建单个而不是多个 Random 对象可以避免此问题。
        要生成适合于创建随机密码的加密安全随机数,请使用从 System.Security.Cryptography.RandomNumberGenerator 派生的类,如 System.Security.Cryptography.RNGCryptoServiceProvider。    所以说存在两种解决方案:一种就是20楼提供的那样,创建单个的 Random 对象;另一种是使用RNGCryptoServiceProvider来提供更加安全可靠的解决方案。
        但是后一种方案的性能相较前者有明显的差距,所以在对随机性要求不高的情况下建议使用第一种解决方案。关于后一种解决方案的参考代码如下:using System;
    using System.Text;
    using System.Security.Cryptography;namespace Yasen.Backus
    {
        public class Program
        {
            public static void Main(String[] args)
            {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("{0:D2}:  " + GetPWD(8), i+1);
                }
            }        public static String GetPWD(int length)
            {
                String[] source = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
                StringBuilder pwd = new StringBuilder(length);            for (int i = 0; i < length; i++)
                {
                    pwd.Append(source[GetRandNum(source.Length)]);
                }
                return pwd.ToString();
            }        public static int GetRandNum(int numSides)
            {
                byte[] bytes = new byte[4];            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetBytes(bytes);            //返回由字节数组中指定位置的四个字节转换来的 32 位整数并保证得到的是正数
                int rand = BitConverter.ToInt32(bytes, 0) & Int32.MaxValue;            return rand % numSides;
            }    }
    }
      

  19.   

    把Random rd = new Random()拿到方法外面去定义
    再加上18楼的方法,应该是不会有重复的了,并且速度很快