問題描述
在 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, ...)
這篇關于什么是游戲的好的隨機數生成器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!