What is a good random number generator for a game?(什么是游戏的好的随机数生成器?)
问题描述
在 C++ 中用于游戏的好的随机数生成器是什么?
我的考虑是:
- 需要大量随机数,所以速度不错.
- 玩家总是会抱怨随机数,但我希望能够向他们指出一个参考资料,说明我确实做到了我的工作.
- 由于这是一个我没有太多时间参与的商业项目,如果算法 a) 相对容易实现或 b) 有一个很好的非 GPL 实现可用,那就太好了.
- 我已经在很多地方使用了
rand()
,所以任何其他生成器最好能证明它需要的所有更改是合理的.
我对这个主题不太了解,所以我能想到的唯一选择是 Mersenne捻线机;它是否满足所有这些要求?还有什么更好的吗?
Mersenne Twister 似乎是共识选择.但是第 4 点呢?它真的比 rand()
好很多吗?
编辑 2: 让我更清楚地说明第 2 点:玩家无法通过知道随机数来作弊.时期.我希望它足够随机,以至于人们(至少那些了解随机性的人)不会抱怨它,但我并不担心预测.这就是为什么我把速度作为首要考虑因素.
编辑 3:我现在倾向于 Marsaglia RNG,但我仍然希望获得更多的输入.因此,我正在设置赏金.
编辑 4: 请注意:我打算在今天 UTC 午夜之前接受一个答案(以避免弄乱某人的代表上限).因此,如果您正在考虑回答,请不要等到最后一刻!
另外,我喜欢 Marsaglia 的 XORshift 生成器的外观.有人对他们有任何意见吗?
George Marsaglia 有开发了一些目前可用的最好和最快的 RNGMultiply-with-carry 是一个值得注意的均匀分布.>
=== 更新 2018-09-12 ===
对于我自己的工作,我现在使用 Xoshiro256**,这是对 Marsaglia 的 XorShift 的一种进化/更新.
=== 更新 2021-02-23 ===
在 .NET 6(目前为预览版)中,System.Random 的实现已更改为使用 xoshiro256**,但仅适用于无参数构造函数.采用种子的构造函数使用旧的 PRNG 以保持向后兼容性.有关详细信息,请参阅提高随机性(性能、API 等)
What is a good random number generator to use for a game in C++?
My considerations are:
- Lots of random numbers are needed, so speed is good.
- Players will always complain about random numbers, but I'd like to be able to point them to a reference that explains that I really did my job.
- Since this is a commercial project which I don't have much time for, it would be nice if the algorithm either a) was relatively easy to implement or b) had a good non-GPL implementation available.
- I'm already using
rand()
in quite a lot of places, so any other generator had better be good to justify all the changes it would require.
I don't know much about this subject, so the only alternative I could come up with is the Mersenne Twister; does it satisfy all these requirements? Is there anything else that's better?
Edit: Mersenne Twister seems to be the consensus choice. But what about point #4? Is it really that much better than rand()
?
Edit 2: Let me be a little clearer on point 2: There is no way for players to cheat by knowing the random numbers. Period. I want it random enough that people (at least those who understand randomness) can't complain about it, but I'm not worried about predictions. That's why I put speed as the top consideration.
Edit 3: I'm leaning toward the Marsaglia RNGs now, but I'd still like more input. Therefore, I'm setting up a bounty.
Edit 4: Just a note: I intend to accept an answer just before midnight UTC today (to avoid messing with someone's rep cap). So if you're thinking of answering, don't wait until the last minute!
Also, I like the looks of Marsaglia's XORshift generators. Does anyone have any input about them?
George Marsaglia has developed some of the best and fastest RNGs currently available Multiply-with-carry being a notable one for a uniform distribution.
=== Update 2018-09-12 ===
For my own work I'm now using Xoshiro256**, which is a sort of evolution/update on Marsaglia's XorShift.
=== Update 2021-02-23 ===
In .NET 6 (currently in preview) the implementation of System.Random has been changed to use xoshiro256**, but only for the parameterless constructor. The constructor that takes a seed uses the old PRNG in order to maintain backwards compatibility. For more info see Improve Random (performance, APIs, ...)
这篇关于什么是游戏的好的随机数生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是游戏的好的随机数生成器?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01