为什么System.Random连续多次给'1',然后不是一段时间,然后呢?

Ash*_*ies 23 .net c# random

不知道怎么解释这个,所以标题几乎描述了这个问题.

Random不会在循环的每个部分重新初始化.它是一个类的静态成员,我总是从其他类调用它.

我没有使用自定义种子.

初始化代码是:

    public static Random random = new Random();

        for (int x = 0; x < 75; x++)
        {
            if (main.random.Next(11) == 1)
            {
                tiles[heightMap[x] - 1][x] = 4;
                tiles[heightMap[x] - 2][x] = 4;
                tiles[heightMap[x] - 3][x] = 4;
                tiles[heightMap[x] - 4][x] = 4;
                tiles[heightMap[x] - 5][x] = 4;
                tiles[heightMap[x] - 5][x - 1] = 5;
                tiles[heightMap[x] - 6][x - 1] = 5;
                tiles[heightMap[x] - 6][x] = 5;
                tiles[heightMap[x] - 5][x + 1] = 5;
                tiles[heightMap[x] - 6][x + 1] = 5;
            }
        }
Run Code Online (Sandbox Code Playgroud)

这(我知道这不是一个好方法 - 它是基本的和临时的)生成一棵树.

但是我的地形通常看起来像这样,有许多聚类树:

☁☁☁☁☁☁☁☁☁☁

任何人都可以深入了解为什么会这样吗?有没有比使用System.Security.Cryptography.Random类更好的选择?

我预计每棵树平均有9个缺口,但它更像7个,然后3个树紧密地聚集在一起.

在此输入图像描述

Mar*_*ell 35

这是一种概率误解; 所有你知道的是,在任何一点上,在下一个时段获得树的机会是,假设均匀分布,1比11.

因此获得0的差距的机会是1/11

因此获得1的差距的机会是10/11*1/11

因此,获得2的差距的机会是10/11*10/11*1/11

等等

所有那些10/11加起来(好吧,倍增)!那么让我们写一个实用程序:

decimal accountedFor = 0M;
for (int i = 0; i <= 20; i++)
{
    decimal chance = 1M / 11M;
    for (int j = 0; j < i; j++) chance *= 10M / 11M;
    accountedFor += chance;
    Console.WriteLine("{0:00}: {1:00.0%}\t({2:00.0%})", i, chance, accountedFor);
}
Run Code Online (Sandbox Code Playgroud)

这使:

00: 09.1%       (09.1%)
01: 08.3%       (17.4%)
02: 07.5%       (24.9%)
03: 06.8%       (31.7%)
04: 06.2%       (37.9%)
05: 05.6%       (43.6%)
06: 05.1%       (48.7%)
07: 04.7%       (53.3%)
08: 04.2%       (57.6%)
09: 03.9%       (61.4%)
10: 03.5%       (65.0%)
11: 03.2%       (68.1%)
12: 02.9%       (71.0%)
13: 02.6%       (73.7%)
14: 02.4%       (76.1%)
15: 02.2%       (78.2%)
16: 02.0%       (80.2%)
17: 01.8%       (82.0%)
18: 01.6%       (83.6%)
19: 01.5%       (85.1%)
20: 01.4%       (86.5%)
Run Code Online (Sandbox Code Playgroud)

这解释了小差距的偏见.注意; 当我们达到20的差距时,我们的概率低于1.5%,并且占所有可能结果的85% - 其余的15%将分布在其余的无穷大(即差距)大小13212是可能的,但非常不可能).

所以这是一个模拟:

int[] gapCounts = new int[21];

int gap = 0;
// simulate a few gaps using your algo
var random = new Random();
for (int x = 0; x < 100000; x++)
{
    if (random.Next(11) == 1)
    { // count that gap
        gapCounts[gap]++;
        gap = 0;
    }
    else
    {
        gap++;
        if(gap >= gapCounts.Length)
        { // just skip anything too large, sorry
            gap = 0;
        }
    }
}

decimal total = gapCounts.Sum();
for(int i = 0 ; i < gapCounts.Length ; i++)
{
    Console.WriteLine("{0:00}: {1:00.0%}", i, gapCounts[i] / total);
}
Run Code Online (Sandbox Code Playgroud)

输出没有这些值会在每次运行时改变:

00: 11.0%
01: 09.4%
02: 08.6%
03: 07.9%
04: 07.3%
05: 06.5%
06: 05.4%
07: 05.4%
08: 04.7%
09: 04.5%
10: 04.4%
11: 03.4%
12: 03.5%
13: 03.0%
14: 02.9%
15: 02.4%
16: 02.5%
17: 02.2%
18: 01.9%
19: 01.5%
20: 01.7%
Run Code Online (Sandbox Code Playgroud)

  • @AshleyDavies我使用你的算法添加了一个模拟; 数据看起来对我来说(即与预测大致一致) (2认同)