本文介绍了为什么我的随机数发生器播种两个阵列使用相同的数字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的方法来创建两个新的 INT
阵列随机数,但两个数组包含完全相同的编号。为什么会出现这种情况?
静态无效的主要(字串[] args)
{
INT [] Foo1 =美孚(1000);
INT [] foo2的=美孚(1000);
}
静态INT []美孚(INT长度)
{
INT []数组=新INT [长度];
随机R =新的随机();
的for(int i = 0; I<长度;我++)
{
阵列[I] = r.Next(1,101);
}
//Thread.Sleep(6);
返回数组;
}
解决方案
你不播种随机但你可能用它足够近调用之间,默认的种子是在两种情况下是相同的:
I'm using a method to create two new int
arrays with random numbers,but the two array contains exactly the same numbers. Why is this happening?
static void Main(string[] args)
{
int[] Foo1= Foo(1000);
int[] Foo2= Foo(1000);
}
static int[] Foo(int length)
{
int[] Array = new int[length];
Random r = new Random();
for (int i = 0; i < length; i++)
{
Array[i]=r.Next(1, 101);
}
//Thread.Sleep(6);
return Array;
}
解决方案
You're not seeding Random but you're likely using it close enough between calls that the default seed is the same in both cases :
这篇关于为什么我的随机数发生器播种两个阵列使用相同的数字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!