http://www.cnblogs.com/djlzxzy/archive/2011/08/10/2134276.html
在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之間的隨機數。
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[10];
int[] b = new int[10];
int[] c = new int[10];
float[] d = new float[10];
int i;
Random ra = new Random(Environment.TickCount);
for (i = 0; i < 10; i++)
{
a[i] = randbit(1, 20, i);
b[i] = randbit(1, 20);
c[i] = ra.Next(1, 20);
d[i] = Convert.ToSingle(ra.NextDouble());
}
for (i = 0; i < 10; i++)
{
Console.WriteLine("a[" + "{0}" + "] = {1}/r", i, a[i]);
}
Console.WriteLine("/r");
for (i = 0; i < 10; i++)
{
Console.WriteLine("b[" + "{0}" + "] = {1}/r", i, b[i]);
}
Console.WriteLine("/r");
for (i = 0; i < 10; i++)
{
Console.WriteLine("c[" + "{0}" + "] = {1}/r", i, c[i]);
}
Console.WriteLine("/r");
for (i = 0; i < 10; i++)
{
Console.WriteLine("d[" + "{0}" + "] = {1}/r", i, d[i]);
}
Console.ReadLine();
}
public static int randbit(int i, int j, int p)
{
int a;
Random ra = new Random(Guid.NewGuid().GetHashCode() + p);
a = ra.Next(i, j);
return a;
}
public static int randbit(int i, int j)
{
int a;
Random ra = new Random(Guid.NewGuid().GetHashCode());
a = ra.Next(i, j);
return a;
}
}
}
得到的結果為:
- a[0] = 8
- a[1] = 13
- a[2] = 13
- a[3] = 17
- a[4] = 5
- a[5] = 1
- a[6] = 15
- a[7] = 14
- a[8] = 16
- a[9] = 3
- b[0] = 9
- b[1] = 13
- b[2] = 11
- b[3] = 1
- b[4] = 2
- b[5] = 15
- b[6] = 5
- b[7] = 11
- b[8] = 6
- b[9] = 13
- c[0] = 9
- c[1] = 16
- c[2] = 6
- c[3] = 1
- c[4] = 1
- c[5] = 14
- c[6] = 14
- c[7] = 12
- c[8] = 17
- c[9] = 18
- d[0] = 0.8177258
- d[1] = 0.998677
- d[2] = 0.6717096
- d[3] = 0.3508099
- d[4] = 0.944403
- d[5] = 0.7056777
- d[6] = 0.1024248
- d[7] = 0.2304256
- d[8] = 0.1107363
- d[9] = 0.5068604
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespacetester
{
///<summary>
///產生不重復隨機數的應用
///鄭少東 2008.08.24
///摘要 C#隨機數的應用中 如果是需要在短時間內產生大量隨機數 推薦使用Guid.NewGuid().GetHashCode()作為種子
///</summary>
classProgram
{
staticvoidMain(string[] args)
{
Console.WriteLine(String.Format("開始時間{0}", DateTime.Now.ToString("yyyy-MM-dd hh:ss fff")));
List<int>Numbers=Program.GetRandom(1,1000);
for(inti=0; i<Numbers.Count;i++)
{
//Console.WriteLine(Numbers[i]);
}
Console.WriteLine(String.Format("結束時間{0}", DateTime.Now.ToString("yyyy-MM-dd hh:ss fff")));
Console.ReadLine();
}
///<summary>
///返回一組唯一不重復的隨機數
///</summary>
///<param name="minValue">最小值</param>
///<param name="maxValue">最大值</param>
///<returns>返回一組唯一不重復的隨機數</returns>
publicstaticList<int>GetRandom(intminValue,intmaxValue)
{
List<int>Numbers=newList<int>();
//使用Guid.NewGuid().GetHashCode()作為種子,可以確保Random在極短時間產生的隨機數盡可能做到不重復
Random rand=newRandom(Guid.NewGuid().GetHashCode());
intitem;
for(inti=minValue; i<=maxValue; i++)
{
item=rand.Next(minValue, maxValue+1);
while(Numbers.IndexOf(item)!=-1)
{
item=rand.Next(minValue, maxValue+1);
}
Numbers.Add(item);
}
returnNumbers;
}
}
}