重新发明轮子:随机数生成器
所以我是 C++ 新手,正在尝试学习一些东西。因此,我正在尝试制作一个随机数生成器(RNG 或 PRNG,如果你愿意的话)。我有 RNG 的基本知识,就像你必须从种子开始,然后通过算法发送种子一样。我所困惑的是人们如何想出上述算法。
这是我必须获取种子的代码。
int getSeed()
{
time_t randSeed;
randSeed = time(NULL);
return randSeed;
}
现在我知道 C++ 中有预先构建的 RNG,但我希望学习的不仅仅是复制其他人的工作并尝试弄清楚它。
因此,如果有人能引导我到哪里可以阅读或向我展示如何为此提出算法的示例,我将非常感激。
So I'm new to C++ and am trying to learn some things. As such I am trying to make a Random Number Generator (RNG or PRNG if you will). I have basic knowledge of RNGs, like you have to start with a seed and then send the seed through the algorithm. 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,需要澄清的是,您提出的任何算法都将是伪随机数生成器,而不是真正的随机数生成器。由于您将制定一个算法(即编写一个函数,即制定一组规则),因此随机数生成器最终必须重复自身或执行类似的非随机操作。
真正的随机数生成器的例子是从自然界捕获随机噪声并将其数字化的生成器。其中包括:
http://www.fourmilab.ch/hotbits/
http://www.random.org/
您还可以购买产生白噪声(或其他随机性手段)和数字捕获的物理设备它:
http://www.lavarnd.org/
http://www.idquantique.com/true-random-number-generator/products-overview.html
http://www.araneus.fi/products-alea-eng.html
就伪随机数生成器而言,最容易学习的(并且普通外行可能可以自己制作的)是 线性同余生成器。不幸的是,这些也是最糟糕的 PRNG 之一。
确定什么是好的 PRNG 的一些准则包括:
现在在大多数应用程序中被认为很好的更流行的之一(即不是密码学)是 < a href="http://en.wikipedia.org/wiki/Mersenne_twister" rel="noreferrer">梅森旋转器。从链接中可以看到,这是一个简单的算法,也许只有 30 行代码。然而,尝试从头开始编写 20 或 30 行代码需要大量的脑力和对 PRNG 的研究。通常最著名的算法是由研究 PRNG 数十年的教授或行业专业人士设计的。
我希望你确实学习 PRNG 并尝试推出自己的(尝试 Knuth 的计算机编程艺术或数值食谱作为起点),但我只是想在一天结束时将这一切都列出来(除非 PRNG 将是你的)一生的工作)最好只是使用别人想出的东西。另外,沿着这些思路,我想指出,历史上编译器、电子表格等不使用大多数数学家认为好的 PRNG,因此,如果您需要高质量的 PRNG,请不要使用标准库在 C++、Excel、.NET、Java 等中,直到您研究它们是用什么实现的。
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 (i.e. writing a function, i.e. making a set of rules), the random number generator would have to eventually repeat itself or do something similar which would be non-random.
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.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.idquantique.com/true-random-number-generator/products-overview.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.
Some guidelines for determining what is a good PRNG include:
One of the more popular ones right now that is considered good in most applications (ie not crptography) is the Mersenne Twister. As you can see from the link, it is a simple algorithm, perhaps only 30 lines of code. However trying to come up with those 20 or 30 lines of code from scratch takes a lot of brainpower and study of PRNGs. Usually the most famous algorithms are designed by a professor or industry professional that has studied PRNGs for decades.
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. 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.
线性同余生成器很常用,Wiki 文章对此进行了很好的解释。
A linear congruential generator is commonly used and the Wiki article explains it pretty well.
引用约翰·冯·诺依曼的话:
这取自高德纳 (Knuth) 的《计算机编程艺术》一书的第 3 章随机数,这一定是对该主题最详尽的概述。一旦你读完它,你就会精疲力竭。您还会知道为什么不想编写自己的随机数生成器。
To quote John von Neumann:
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. 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:
populated with "real" random values.
calculating the starting index with
system time.
the value for each call to your
function.