两个不同的种子生产相同“随机”序列 [英] Two different seeds producing the same 'random' sequence

查看:165
本文介绍了两个不同的种子生产相同“随机”序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有这种非常逻辑的解释,但我似乎无法理解为什么种子 0 2,147,483,647 产生相同的随机序列,使用.NET的 Random类(系统)

Maybe there is a very logic explanation for this, but I just can't seem to understand why the seeds 0 and 2,147,483,647 produce the same "random" sequence, using .NET's Random Class (System).

快速的代码示例:

ushort len = 8;
Random r0 = new Random(0), r1 = new Random(1), r2 = new Random(int.MaxValue); //2,147,483,647
byte[] b0 = new byte[len], b1 = new byte[len], b2 = new byte[len];

r0.NextBytes(b0);
r1.NextBytes(b1);
r2.NextBytes(b2);

for (int i = 0; i < len; i++)
{
  System.Diagnostics.Debug.WriteLine("{0}\t\t{1}\t\t{2}", b0[i], b1[i], b2[i]);
}

Console.ReadLine();



输出:

Output:

26      70      26
12      208     12
70      134     76
111     130     111
93      64      93
117     151     115
228     228     228
216     163     216

正如你所看到的,第一和第三顺序是一样的。是否有人可以给我讲解一下

As you can see, the first and the third sequence are the same. Can someone please explain this to me?

修改:显然,作为ALRO指出的那样,这些序列是不一样的。但他们都非常相似。

EDIT: Apparently, as alro pointed out, these sequences are not the same. But they are very similar.

推荐答案

好了,之所以会与任何派生功能所使用的Random类连接派生从种子的伪随机序列。在真正的答案,因此,这是数学(并超出了我的能力)

Well, the reason will be connected with whatever derivation function is used by the Random class to derive a pseudo-random sequence from the seed. The real answer, therefore, is mathematical (and beyond my ability).

事实上 - 我不。相信有任何保证两个不同的种子必然产生不同的序列,反正

Indeed - I don't believe there's any guarantee that two different seeds will necessarily produce different sequences anyway.

修改好吧 - 我要做的已经做了什么bitbonk - 但解释的为什么的:

Edit Okay - I'm going to do what bitbonk has done - but explain why:

public Random(int Seed)
{
    int num = (Seed == -2147483648) ? 2147483647 : Math.Abs(Seed);
    int num2 = 161803398 - num;
    this.SeedArray[55] = num2;
    int num3 = 1;
    for (int i = 1; i < 55; i++)
    {
        int num4 = 21 * i % 55;
        this.SeedArray[num4] = num3;
        num3 = num2 - num3;
        if (num3 < 0)
        {
            num3 += 2147483647;
        }
        num2 = this.SeedArray[num4];
    }
    for (int j = 1; j < 5; j++)
    {
        for (int k = 1; k < 56; k++)
        {
            this.SeedArray[k] -= this.SeedArray[1 + (k + 30) % 55];
            if (this.SeedArray[k] < 0)
            {
                this.SeedArray[k] += 2147483647;
            }
        }
    }
    this.inext = 0;
    this.inextp = 21;
    Seed = 1;
} 

我们实际上并不需要到太远进入代码,看看为什么 - 阅读代码从上到下这些都是由上面的代码时,种子被存储的值 0 当种子 2147483647

We don't actually need to go too far into the code to see why - reading the code from top to bottom these are the values that will be stored by the above code when the seed is 0 and when the seed is 2147483647:

int num = (Seed == -2147483648) ? 2147483647 : Math.Abs(Seed);
  =>  num is 0 and 2147483647

int num2 = 161803398 - num;
  => num2 is 161803398 and -1985680249

this.SeedArray[55] = num2;
  => this.SeedArray is as above in both cases

int num3 = 1;
for (int i = 1; i < 55; i++)
{
  int num4 = 21 * i % 55
  this.SeedArray[num4] = num3;

  => num4 is 21, SeedArray[21] is 1

num3 = num2 - num3
  => num3 is 161803397 and -1985680250

if(num3 < 0)
  num3 += 2147483647

  => num3 is 161803397 and 161803397



只是第一个循环后,算法已经收敛了两个种子值

After just the very first loop, algorithm has already converged for the two seed values.

修改

正如已经指出了这个问题 - 序列是不一样的 - 但他们显然非常非常相似 - 在这里,我们可以看到这种相似的原因

As has been pointed out on the question - the sequences aren't the same - but they are clearly very very similar - and here we can see the reason for that similarity.

这篇关于两个不同的种子生产相同“随机”序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆