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

发布于 2024-11-04 06:34:54 字数 367 浏览 4 评论 0原文

所以我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悸初 2024-11-11 06:34:54

首先,需要澄清的是,您提出的任何算法都将是伪随机数生成器,而不是真正的随机数生成器。由于您将制定一个算法(即编写一个函数,即制定一组规则),因此随机数生成器最终必须重复自身或执行类似的非随机操作。

真正的随机数生成器的例子是从自然界捕获随机噪声并将其数字化的生成器。其中包括:

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 的一些准则包括:

  1. 周期性(可用数字的范围是多少?)
  2. 连续数字(同一数字连续重复两次的概率是多少)
  3. 均匀性(选择的可能性是否相同)某个子范围中的数字作为另一个子范围)
  4. 对其进行逆向工程的困难(如果它接近真正的随机,那么有人应该无法根据它生成的最后几个数字计算出它生成的下一个数字)
  5. 速度(如何我可以快速生成一个新数字吗?它需要 5 或 500 次算术运算吗)
  6. 我确信我还缺少其他一些

现在在大多数应用程序中被认为很好的更流行的之一(即不是密码学)是 < 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:

  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)
  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. 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.

梦一生花开无言 2024-11-11 06:34:54

线性同余生成器很常用,Wiki 文章对此进行了很好的解释。

A linear congruential generator is commonly used and the Wiki article explains it pretty well.

空‖城人不在 2024-11-11 06:34:54

引用约翰·冯·诺依曼的话:

任何考虑算术的人
产生随机数字的方法是
当然是在罪恶的状态下。

这取自高德纳 (Knuth) 的《计算机编程艺术》一书的第 3 章随机数,这一定是对该主题最详尽的概述。一旦你读完它,你就会精疲力竭。您还会知道为什么不想编写自己的随机数生成器。

To quote John von Neumann:

Anyone who considers arithmetical
methods of producing random digits is
of course in a state of sin.

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.

究竟谁懂我的在乎 2024-11-11 06:34:54

正确的解决方案最能满足要求,并且每种情况的要求都是独特的。这可能是最简单的方法:

  • 创建一个大的一维数组
    填充“真实”随机值。
  • 通过以下方式“播种”你的伪随机生成器
    计算起始索引
    系统时间。
  • 遍历数组并返回
    每次致电您的价值
    功能。
  • 到达末端时绕一圈。

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:

  • 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.
  • Wrap around when it reaches the end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文