简体   繁体   English

重新发明轮子:随机数生成器

[英]Reinventing The Wheel: Random Number Generator

So I'm new to C++ and am trying to learn some things.所以我是 C++ 的新手,我正在尝试学习一些东西。 As such I am trying to make a Random Number Generator (RNG or PRNG if you will).因此,我正在尝试制作一个随机数生成器(如果您愿意,可以使用 RNG 或 PRNG)。 I have basic knowledge of RNGs, like you have to start with a seed and then send the seed through the algorithm.我有 RNG 的基本知识,比如你必须从种子开始,然后通过算法发送种子。 What I'm stuck at is how people come up with said algorithms.我坚持的是人们如何提出上述算法。

Here is the code I have to get the seed.这是我必须获得种子的代码。

int getSeed()
{
    time_t randSeed;
    randSeed = time(NULL);
    return randSeed;
}

Now I know that there is are prebuilt RNGs in C++ but I'm looking to learn not just copy other people's work and try to figure it out.现在我知道 C++ 中有预构建的 RNG,但我希望学习的不仅仅是复制其他人的工作并试图弄清楚。

So if anyone could lead me to where I could read or show me examples of how to come up with algorithms for this I would be greatly appreciative.因此,如果有人能引导我到哪里可以阅读或向我展示如何为此提出算法的示例,我将不胜感激。

First, just to clarify, any algorithm you come up with will be a pseudo random number generator and not a true random number generator.首先,澄清一下,您提出的任何算法都是伪随机数生成器,而不是真正的随机数生成器。 Since you would be making an algorithm (ie writing a function, ie making a set of rules), the random number generator would have to eventually repeat itself or do something similar which would be non-random.由于您要制定算法(即编写 function,即制定一组规则),随机数生成器最终将不得不重复自身或执行类似的非随机操作。

Examples of truly random number generators are one's that capture random noise from nature and digitize it.真正随机数生成器的示例是从自然界捕获随机噪声并将其数字化的生成器。 These include:这些包括:

http://www.fourmilab.ch/hotbits/ http://www.fourmilab.ch/hotbits/

http://www.random.org/ http://www.random.org/

You can also buy physical equipment that generate white noise (or some other means on randomness) and digitally capture it:您还可以购买产生白噪声(或其他一些随机方式)的物理设备并以数字方式捕获它:

http://www.lavarnd.org/ http://www.lavarnd.org/

http://www.idquantique.com/true-random-number-generator/products-overview.html http://www.idquantique.com/true-random-number-generator/products-overview.html

http://www.araneus.fi/products-alea-eng.html http://www.araneus.fi/products-alea-eng.html

In terms of pseudo random number generators, the easiest ones to learn (and ones that an average lay person could probably make on their own) are the linear congruential generators .就伪随机数生成器而言,最容易学习的(以及普通外行可能自己制作的)是线性同余生成器 Unfortunately, these are also some of the worst PRNGs there are.不幸的是,这些也是一些最糟糕的 PRNG。

Some guidelines for determining what is a good PRNG include:确定什么是好的 PRNG 的一些准则包括:

  1. Periodicity (what is the range of available numbers?)周期性(可用数字的范围是多少?)
  2. Consecutive numbers (what is the probability that the same number will be repeated twice in a row)连续数(同一个数连续重复两次的概率是多少)
  3. Uniformity (Is it just as likely to pick numbers from a certain sub range as another sub range)一致性(从某个子范围中选择数字的可能性是否与从另一个子范围中选择数字的可能性一样)
  4. Difficulty in reverse engineering it (If it is close to truly random then someone should not be able to figure out the next number it generates based on the last few numbers it generated)逆向工程的难度(如果它接近于真正随机,那么有人应该无法根据它生成的最后几个数字找出它生成的下一个数字)
  5. Speed (how fast can I generate a new number? Does it take 5 or 500 arithmetic operations)速度(我能以多快的速度生成一个新数字?需要 5 次还是 500 次算术运算)
  6. I'm sure there are others I am missing我确定还有其他我想念的

One of the more popular ones right now that is considered good in most applications (ie not crptography) is the Mersenne Twister . Mersenne Twister是目前在大多数应用程序中被认为很好(即不是 crptography)的比较流行的一种。 As you can see from the link, it is a simple algorithm, perhaps only 30 lines of code.从链接可以看出,它是一个简单的算法,可能只有 30 行代码。 However trying to come up with those 20 or 30 lines of code from scratch takes a lot of brainpower and study of PRNGs.然而,试图从头开始编写那 20 或 30 行代码需要大量的脑力和对 PRNG 的研究。 Usually the most famous algorithms are designed by a professor or industry professional that has studied PRNGs for decades.通常最著名的算法是由研究 PRNG 数十年的教授或行业专业人士设计的。

I hope you do study PRNGs and try to roll your own (try Knuth's Art of Computer Programming or Numerical Recipes as a starting place), but I just wanted to lay this all out so at the end of the day (unless PRNGs will be your life's work) its much better to just use something someone else has come up with.我希望你学习 PRNG 并尝试自己动手(尝试 Knuth 的计算机编程艺术或数字食谱作为起点),但我只是想在一天结束时把这一切都说清楚(除非 PRNG 将成为你的生活的工作)最好只使用别人想出的东西。 Also, along those lines, I'd like to point out that historically compilers, spreadsheets, etc. don't use what most mathematicians consider good PRNGs so if you have a need for a high quality PRNGs don't use the standard library one in C++, Excel, .NET, Java, etc. until you have research what they are implementing it with.另外,沿着这些思路,我想指出历史上的编译器、电子表格等不使用大多数数学家认为好的 PRNG,所以如果您需要高质量的 PRNG,请不要使用标准库之一在 C++、Excel、.NET、ZD52387880E1EA22817A72D375921381Z 等中,他们正在研究什么。

A linear congruential generator is commonly used and the Wiki article explains it pretty well.通常使用线性同余生成器, Wiki 文章对此进行了很好的解释。

To quote John von Neumann:引用约翰·冯·诺依曼的话:

Anyone who considers arithmetical methods of producing random digits is of course in a state of sin.任何考虑产生随机数字的算术方法的人当然是在罪的 state 中。

This is taken from Chapter 3 Random Numbers of Knuth's book "The Art of Computer Programming", which must be the most exhaustive overview of the subject available.这摘自 Knuth 的书“计算机编程的艺术”的第 3 章随机数,这必须是对该主题最详尽的概述。 And once you have read it, you will be exhausted.而一旦你读过它,你会筋疲力尽。 You will also know why you don't want to write your own random number generator.您还将知道为什么不想编写自己的随机数生成器。

The correct solution best fulfills the requirements and the requirements of every situation will be unique.正确的解决方案最能满足要求,并且每种情况的要求都是独一无二的。 This is probably the simplest way to go about it:这可能是 go 关于它的最简单方法:

  • Create a large one dimensional array populated with "real" random values.创建一个用“真实”随机值填充的大型一维数组。
  • "seed" your pseudo-random generator by calculating the starting index with system time.通过使用系统时间计算起始索引来“播种”您的伪随机生成器。
  • Iterate through the array and return the value for each call to your function.遍历数组并将每次调用的值返回给您的 function。
  • Wrap around when it reaches the end.到达终点时绕一圈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM