C++强大的随机数生成器 - 有必要吗?
我最新的项目是开发一款基于文本的角色扮演游戏,现在我需要一个随机数生成器,它是一个很好的随机数生成器,可以在不同情况下计算某些动作是否可以顺利执行。我们每个人都知道,基本函数 std::srand
和 std::rand
是计算伪随机值的简单算法。但是我想要真实的值而不是伪值。因此我想问,使用比上述解决方案更好的解决方案并坚持基础知识是否会太过分?如果没有,你有什么建议?如何实现这样一个“好的生成器”呢?
My newest project is the development of a text based RPG and now I need a random number generator, a good one to compute in different situations whether some action can be performed without problems or not. Everyone of us knows, that the basic functions std::srand
and std::rand
are simple and easy algorithms to compute pseudo random values. However I want to have real and not pseudo values. Therefore i want to ask, whether it would to much overkill using a better solution than the mentioned one and to stick to the basics; and if not, what would you suggest? How to implement such a "good generator"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我的建议是使用 Boost.Random。它有一个 数量相当不错(并且快速)RNG。您不需要加密安全的加密货币,但他们提供的加密货币比
rand
更好。我自己会选择mt19937。它的周期很长,而且速度很快。但是 Boost.Random 有很多这样的东西,可以满足您的大多数非加密安全需求。
My suggestion is to use Boost.Random. It has a number of quite good (and fast) RNGs. You don't need a cryptographically secure one, but the ones they offer are better than
rand
.I'd go with mt19937 myself. It has a long period and is pretty fast. But Boost.Random has lots of these things, for most of your non-cryptographically secure needs.
真正的问题是......有人知道你的伪随机数和真实随机数之间的区别吗?我认为没有人会。您默认找到的库足够健全,您的用户将永远无法找到任何模式。
The real question is... will anyone know the difference between your pseudo and real random numbers? I don't think anyone will. The libraries you find by default are sound enough that your users will never be able to find any patterns.
也许您对几个不同的概念感到困惑。
一个概念是不可预测性:由于 PRGN 基于确定性算法和单个种子值,因此可以根据对先前数字的观察来预测下一个“随机”数字。这是密码学中的一个大问题,因此为了避免这种情况,您可以从一些真正的熵源(例如
/dev/random
)中选择一个“真实”随机数。但是,这仅对一个随机数有用。另一个概念是概率分布。如果您希望数字在某个时间间隔内均匀分布,则需要一种方法来正确实现此目的,否则您的随机事件将出现偏差。这与不可预测性无关,并且相当可预测的伪 RNG 可能完全适合生成统计上正确的均匀(或任何派生)分布。
由于您的游戏机制几乎肯定取决于随机事件的良好统计特性,因此您应该主要关注选择一个好的伪 RNG,然后从足够随机的源(可能是
/dev/random)。在需要控制随机事件的统计属性的游戏中,真正的随机性并不好。
Maybe you are confused about several distinct concepts.
One concept is unpredictability: Since a PRGN is based on deterministic algorithms and a single seed value, it is possible to predict the next "random" number based on observations of previous numbers. This is a huge problem in cryptography, so to avoid this, you would pick a "true" random number from some genuine entropy source such as
/dev/random
. However, this is only useful for one single random number.The other concept is that of a probability distribution. If you want numbers uniformly distributed over an interval, you need a method to achieve this correctly, or your random events will come out as skewed. This isn't related to unpredictability, and a rather predictable pseudo-RNG may be entirely suitable for producing a statistically correct uniform (or any derived) distribution.
Since your game mechanics will almost surely depend on good statistical properties of the random events, you should focus primarily on picking a good pseudo-RNG, and then seed this from a sufficiently random source (maybe
/dev/random
). True randomness is no good in a game where you need control over the statistical properties of random events.根据个人经验,这取决于您如何访问随机数。如果您非常快速地连续生成许多随机数,那么您很可能需要更复杂的东西(例如创建一个大的随机值向量)。然而,对于典型的 RPG 游戏,标准 RNG 应该没问题。
From personal experience, it depends how you're accessing the random numbers. If you're generating many random numbers one after the other in very quick succession, then chances are you'll want something more complex (e.g. creating a large vector of random values). For a typical RPG game however, the standard RNGs should be fine.
由于计算机是确定性的,因此任何随机数生成器都是伪随机的。然而,有些算法比其他算法更好。
对于游戏来说,内置的 std::rand 功能绰绰有余。更复杂的随机数生成器的唯一真正应用是加密。
Since computers are deterministic, any random number generator is pseudo random. However, some algorithms are better than others.
For a game, the built in std::rand functions more than enough. The only real applications for more complex random number generators is encryption.
首先,每个用软件实现的随机数生成器都是伪随机的,您需要依靠一些物理现象(如放射性衰变)来保证(通过现代物理学)随机性。但在大多数应用程序(我不熟悉你的)中,计算简单的伪随机生成器的随机性是完全可以接受的。 TR1中添加了很多这样的设施,无需重新发明轮子,只需查看这篇文章:随机数使用 C++ TR1 生成
First of all, every random number generator implemented in software is pseudo-random, you would need to rely on some physical phenomena (like radioactive decay) to get guaranteed (by modern physics) randomness. But in most applications (I'm not familiar with your) randomness of computationally simple pseudo-random generators is quite acceptable. There were a number of those facilities added in TR1, no need to reinvent the wheel, just check this article: Random number generation using C++ TR1
在linux中,
/dev/random
是一个很好的解决方案。对于真实随机值,您不能仅使用程序。软件无法生成真正的随机值。但真正的随机性几乎不需要。
您能更具体地说明一下您需要随机数的用途吗?
In linux,
/dev/random
is a good solution.For real random values, you can't use just a program. Software is unable to generate true random values. But true randomness is hardly ever needed.
Could you be more specific what you need the random numbers for?