djlzxzy

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

http://blog.csdn.net/xujinpeng99/article/details/6387782

在C#编程中,经常会碰到产生随机数的情况,并且是在短时间内产生一组随机数。如果这组随机数中有大量重复的,则达不到我们的要求。生成随机数可以用伪随机数发生器Random,受种子控制生成伪随机数,默认以当前时间值为种子。如果程序运行的很快,就会导致在几乎同一时刻运行多次,肯定会有重复的。比如我们要生成1到10之间的5个随机数,则经常会产生 2 2 1 1 1这样的情况,那么如何得到非常随机的不那么重复的随机数呢?比如 4 2 3 3 5这样的。

 

        有人说用Thread.Sleep(5) ,但我不推荐,因为这样会使系统减缓运行。

        我采取的方法是:用种子Guid.NewGuid().GetHashCode(),在短时间里不会出现大量重复。

       以下代码中,得到的是1到20之间的10个随机数(不包括20)。数组a、b、c分别采用不同的方法产生随机数,数组a和b均调用了方法randbit,不同的是数组a多传了一个参数i改变随机数的种子,数组b用的方法是我在编程中经常用到的,即通过调用一个方法来产生随机数,非常方便。数组c采用的方法也可以,但在实际编程中很少用到。数组d类似于数组c,只是产生的是0,1之间的随机数。

代码如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication12  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int[] a = new int[10];  
  13.             int[] b = new int[10];  
  14.             int[] c = new int[10];  
  15.             float[] d = new float[10];  
  16.             int i;  
  17.             Random ra = new Random(Environment.TickCount);  
  18.             for (i = 0; i < 10; i++)  
  19.             {  
  20.                 a[i] = randbit(1, 20, i);  
  21.                 b[i] = randbit(1, 20);  
  22.                 c[i] = ra.Next(1, 20);  
  23.                 d[i] = Convert.ToSingle(ra.NextDouble());  
  24.             }  
  25.             for (i = 0; i < 10; i++)  
  26.             {  
  27.                 Console.WriteLine("a[" + "{0}" + "] = {1}/r", i, a[i]);  
  28.             }  
  29.             Console.WriteLine("/r");  
  30.             for (i = 0; i < 10; i++)  
  31.             {  
  32.                 Console.WriteLine("b[" + "{0}" + "] = {1}/r", i, b[i]);  
  33.             }  
  34.             Console.WriteLine("/r");  
  35.             for (i = 0; i < 10; i++)  
  36.             {  
  37.                 Console.WriteLine("c[" + "{0}" + "] = {1}/r", i, c[i]);  
  38.             }  
  39.             Console.WriteLine("/r");  
  40.             for (i = 0; i < 10; i++)  
  41.             {  
  42.                 Console.WriteLine("d[" + "{0}" + "] = {1}/r", i, d[i]);  
  43.             }  
  44.             Console.ReadLine();  
  45.         }  
  46.         public static int randbit(int i, int j, int p)  
  47.         {  
  48.             int a;  
  49.             Random ra = new Random(Guid.NewGuid().GetHashCode() + p);  
  50.             a = ra.Next(i, j);  
  51.             return a;  
  52.         }  
  53.   
  54.         public static int randbit(int i, int j)  
  55.         {  
  56.             int a;  
  57.             Random ra = new Random(Guid.NewGuid().GetHashCode());  
  58.             a = ra.Next(i, j);  
  59.             return a;  
  60.         }  
  61.   
  62.     }  
  63. }  

 

得到的结果为:

 

  1. a[0] = 8  
  2. a[1] = 13  
  3. a[2] = 13  
  4. a[3] = 17  
  5. a[4] = 5  
  6. a[5] = 1  
  7. a[6] = 15  
  8. a[7] = 14  
  9. a[8] = 16  
  10. a[9] = 3  
  11.   
  12. b[0] = 9  
  13. b[1] = 13  
  14. b[2] = 11  
  15. b[3] = 1  
  16. b[4] = 2  
  17. b[5] = 15  
  18. b[6] = 5  
  19. b[7] = 11  
  20. b[8] = 6  
  21. b[9] = 13  
  22.   
  23. c[0] = 9  
  24. c[1] = 16  
  25. c[2] = 6  
  26. c[3] = 1  
  27. c[4] = 1  
  28. c[5] = 14  
  29. c[6] = 14  
  30. c[7] = 12  
  31. c[8] = 17  
  32. c[9] = 18  
  33.   
  34. d[0] = 0.8177258  
  35. d[1] = 0.998677  
  36. d[2] = 0.6717096  
  37. d[3] = 0.3508099  
  38. d[4] = 0.944403  
  39. d[5] = 0.7056777  
  40. d[6] = 0.1024248  
  41. d[7] = 0.2304256  
  42. d[8] = 0.1107363  
  43. d[9] = 0.5068604  

 

以下是我参考的别人的代码:

  1. usingSystem;     
  2. usingSystem.Collections.Generic;     
  3. usingSystem.Text;     
  4.     
  5. namespacetester     
  6. {     
  7.    ///<summary>     
  8.    ///产生不重复随机数的应用     
  9.    ///郑少东 2008.08.24     
  10.    ///摘要 C#随机数的应用中 如果是需要在短时间内产生大量随机数 推荐使用Guid.NewGuid().GetHashCode()作为种子      
  11.    ///</summary>     
  12.    classProgram     
  13.     {     
  14.        staticvoidMain(string[] args)     
  15.         {     
  16.             Console.WriteLine(String.Format("开始时间{0}", DateTime.Now.ToString("yyyy-MM-dd hh:ss fff")));     
  17.             List<int>Numbers=Program.GetRandom(1,1000);     
  18.            for(inti=0; i<Numbers.Count;i++)     
  19.             {     
  20.                //Console.WriteLine(Numbers[i]);     
  21.             }     
  22.             Console.WriteLine(String.Format("结束时间{0}", DateTime.Now.ToString("yyyy-MM-dd hh:ss fff")));     
  23.             Console.ReadLine();     
  24.         }     
  25.     
  26.        ///<summary>     
  27.        ///返回一组唯一不重复的随机数     
  28.        ///</summary>     
  29.        ///<param name="minValue">最小值</param>     
  30.        ///<param name="maxValue">最大值</param>     
  31.        ///<returns>返回一组唯一不重复的随机数</returns>     
  32.        publicstaticList<int>GetRandom(intminValue,intmaxValue)     
  33.         {     
  34.             List<int>Numbers=newList<int>();     
  35.            //使用Guid.NewGuid().GetHashCode()作为种子,可以确保Random在极短时间产生的随机数尽可能做到不重复     
  36.             Random rand=newRandom(Guid.NewGuid().GetHashCode());     
  37.            intitem;     
  38.            for(inti=minValue; i<=maxValue; i++)     
  39.             {     
  40.                 item=rand.Next(minValue, maxValue+1);     
  41.                while(Numbers.IndexOf(item)!=-1)     
  42.                 {     
  43.                     item=rand.Next(minValue, maxValue+1);     
  44.                 }     
  45.                 Numbers.Add(item);     
  46.             }     
  47.            returnNumbers;     
  48.         }     
  49.     }     
  50. }     

 

posted on 2011-08-10 22:28  djlzxzy  阅读(859)  评论(0编辑  收藏  举报