使用随机数作为参数生成随机数

using a random number as a parameter to generate a random number

我不确定这是可以接受的 post,但出于好奇

Random rnd = new Random();
        int random1 = rnd.Next(1, 24);
        int random2 = rnd.Next(25, 49);

        int random3 = rnd.Next(random1, random2);

        int random4 = rnd.Next(50, 74);
        int random5 = rnd.Next(75, 100);

        int random6 = rnd.Next(random4, random5);

        int random7 = rnd.Next(random3, random6);
        Console.WriteLine(random7);

不仅仅是说一个随机数

Random rnd = new Random();
int random1 = rnd.Next(1, 100);
Console.WriteLine(random1);

第一种方法产生的结果更像是曲线分布而不是线性分布。

尝试运行以下命令行应用程序,您会发现不同之处:

using System;

namespace Demo
{
    class Program
    {
        const int N = 1000000;

        static void Main()
        {
            var result1 = testRandom(randomA);
            var result2 = testRandom(randomB);

            Console.WriteLine("Results for randomA():\n");
            printResults(result1);

            Console.WriteLine("\nResults for randomB():\n");
            printResults(result2);
        }

        static void printResults(int[] results)
        {
            for (int i = 0; i < results.Length; ++i)
            {
                Console.WriteLine(i + ": " + new string('*', (int)(results[i]*2000L/N)));
            }
        }

        static int[] testRandom(Func<Random, int> gen)
        {
            Random rng = new Random(12345);

            int[] result = new int[100];

            for (int i = 0; i < N; ++i)
                ++result[gen(rng)];

            return result;
        }

        static int randomA(Random rng)
        {
            return rng.Next(1, 100);
        }

        static int randomB(Random rnd)
        {
            int random1 = rnd.Next(1, 24);
            int random2 = rnd.Next(25, 49);

            int random3 = rnd.Next(random1, random2);

            int random4 = rnd.Next(50, 74);
            int random5 = rnd.Next(75, 100);

            int random6 = rnd.Next(random4, random5);

            return rnd.Next(random3, random6);
        }
    }
}

简单测试直方图)将显示实际分布

private static Random rnd = new Random();

private static int[] Hist() {
  int[] freqs = new int[100];

  // 100 buckets, 1000000 samples; we might expect about 10000 values in each bucket
  int n = 1000000;

  for (int i = 0; i < n; ++i) {
    int random1 = rnd.Next(1, 24);
    int random2 = rnd.Next(25, 49);

    int random3 = rnd.Next(random1, random2);

    int random4 = rnd.Next(50, 74);
    int random5 = rnd.Next(75, 100);

    int random6 = rnd.Next(random4, random5);

    int random7 = rnd.Next(random3, random6);

    freqs[random7] = freqs[random7] + 1;
  }

  return freqs;
}

...

Console.Write(string
 .Join(Environment.NewLine, Hist()
   .Select((v, i) => $"{i,2}: {v,5}");

你会得到类似

的东西
 0:      0 <- OK, zero can't appear
 1:     21 <- too few (about 10000 expected)
 2:     56 <- too few (about 10000 expected)
 3:    125 ...
 4:    171
 5:    292
 6:    392
 7:    560
 8:    747 ...
 9:    931 <- too few (about 10000 expected)
 ...
 45: 21528 <- too many (about 10000 expected)
 46: 21549 ...
 47: 21676
 48: 21699
 49: 21432
 50: 21692
 51: 21785
 52: 21559
 53: 21047
 54: 20985 ...
 55: 20820 <- too many (about 10000 expected)
 ...
 90:   623 <- too few (about 10000 expected)
 91:   492 ...
 92:   350
 93:   231
 94:   173
 95:    88
 96:    52
 97:    13
 98:     0 ...
 99:     0 <- too few (about 10000 expected)

不像均匀分布的随机值,远非如此,而是一种钟形曲线

您的问题假定存在一定程度的随机性。这是不正确的,随机性是一种二元状态。如果无法确定地预测试验的结果,则试验是 随机 。否则我们说它是确定性的。以此类推,你会问哪个死的更多,是被枪杀的还是被电死的?死了就死了!(*)

我们用分布来描述随机性,它描述了各种结果的相对可能性。例如均匀分布、高斯分布、三角形分布、泊松分布或指数分布等等。它们都产生不同的结果落在不同范围内的可能性,但我认识的概率论者不会说均匀分布比高斯分布更随机,反之亦然。同样,您的两种算法将产生不同的结果分布,但由于它们都无法确定地预测,因此它们都符合随机性。

如果你想捕捉可预测性的程度,你可能应该问哪个算法具有更高的 entropy 而不是哪个更随机。一个众所周知的结果是,均匀分布在 class 分布中具有最大熵,并支持有界区间。因此,您的复杂算法比简单的均匀分布具有更低的熵,并且更可预测。

(*) - 除了 "The Princess Bride," Wesley 只有 "mostly dead."