| 
 
  默认情况下,.NET的随机数是根据系统时间来生成的,如果电脑速度很快的话,生成的随机数就会一样。 
  
  Random rnd = new Random();  
  
  int rndNum = rnd.Next();         //int 取值范围内的随机数  
  
  int rndNum = rnd.Next(10);       //得0~9的随机数  
  
  int rndNum = rnd.Next(10,20);    //得10~19的随机数  
  
  int rndNum = rnd.NextDouble();   //得0~1的随机数   
  
  若随机种子为系统时间,用循环一次生成多个随机数. 
  
  因为CPU运算速度太快了,所以每次取到的都是同一个时间.即生成的数字都一样了. 
  
  所以要不停地变换种子. 
  
          public string GetRandomCode() 
  
           { 
  
               char[] chars = { 
  
                                  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 
  
                                  'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' 
  
                              }; 
  
 
               string code = string.Empty; 
  
 
               for (int i = 0; i < 4; i++) 
  
               { 
  
                   //这里是关键,传入一个seed参数即可保证生成的随机数不同             
  
                   //Random rnd = new Random(unchecked((int)DateTime.Now.Ticks)); 
  
                   Random rnd = new Random(GetRandomSeed( )); 
  
                   code += chars[rnd.Next(0, 30)].ToString(); 
  
               } 
  
 
               return code; 
  
           } 
  
 
          /// <summary> 
  
          /// 加密随机数生成器 生成随机种子 
  
          /// </summary> 
  
          /// <returns></returns> 
  
      static int GetRandomSeed() 
  
      { 
  
          byte[] bytes = new byte[4]; 
  
          System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); 
  
          rng.GetBytes(bytes); 
  
          return BitConverter.ToInt32(bytes, 0); 
  
      } 
  
  获取指定数量的随机组合 洗牌程序 思路 
  
      public static IList<string> CreateChargeCodeNo(string PromotionChargeCodeNo, int Count) 
  
      { 
  
          List<string> lis = new List<string>(); 
  
          if (string.IsNullOrEmpty(PromotionChargeCodeNo)) 
  
          { 
  
              return lis; 
  
          } 
  
          string ChargeCodeNo = PromotionChargeCodeNo; 
  
          int length = 10 - PromotionChargeCodeNo.Length; 
  
          while (lis.Count < Count) 
  
          { 
  
              int[] numbers = new int[length * 2]; 
  
              for (int i = 0; i < length * 2; i++) 
  
                  numbers = i + 1; 
  
              for (int i = 0; i < length * 2; i++)//二倍体洗牌 
  
              { 
  
                  Random rand = new Random(GetRandomSeed()); 
  
                  int temp = rand.Next(length * 2); 
  
                  int tempNumber = numbers; 
  
                  numbers = numbers[temp]; 
  
                  numbers[temp] = tempNumber; 
  
              } 
  
              string code = ""; 
  
              for (int x = 0; code.Length < length; x++) 
  
              { 
  
                  code += numbers[x]; 
  
              } 
  
              code = code.Substring(0, length); 
  
              string s = ChargeCodeNo + code; 
  
              if (lis.Contains(s)) 
  
              { 
  
                  continue; 
  
              } 
  
              lis.Add(ChargeCodeNo + code); 
  
          } 
  
          return lis; 
  
      } 
  |