您的位置:首页 > 编程语言 > C#

C#短时间内产生不大量重复的随机数

2015-01-14 09:46 836 查看
在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之间的随机数。

代码如下:

[c-sharp] view
plaincopy

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;

}

}

}

得到的结果为:

[c-sharp] view
plaincopy

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

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

[c-sharp] view
plaincopy

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;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: