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;
}
}
}
有人说用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;
}
}
}
相关文章推荐
- C#短时间内产生大量不重复的随机数
- C#短时间内产生不大量重复的随机数
- C#短时间内产生不大量重复的随机数
- C# Random对象短时间内产生不重复的随机数
- //用于产生m个在0-N范围内的无重复的随机数
- C#产生不重复随机数
- 产生不重复的10个随机数
- PHP产生不重复随机数的5个方法总结
- 产生100个100以内不重复随机数的代码
- 产生100以内的不重复随机数
- 高效产生一组不重复的随机数(受编程珠矶启示)java实现
- 产生n个,从n到n的 不重复随机数集合
- 短时间生成大量不重复随机数字
- PHP产生不重复随机数的5个方法总结
- 产生不重复的随机数
- PHP:产生不重复随机数的方法
- C# 不重复随机数的产生问题
- 高效产生m个n范围内的不重复随机数(m<=n)
- 集合框架-产生10个1-20之间的随机数,要求随机数不能重复
- 关于产生不重复随机数的算法