What is a good random number generator for a game?

前端 未结 16 1543
渐次进展
渐次进展 2020-12-02 04:26

What is a good random number generator to use for a game in C++?

My considerations are:

  1. Lots of random numbers are needed, so speed is good.
  2. P
相关标签:
16条回答
  • 2020-12-02 05:22

    Mersenne Twister is very good, and it's fast as well. I used it in a game and it's not hard at all to implement or use.

    The WELL random algorithm was designed as an improvement over the Mersenne Twister. Game Gems 7 has more info. on it, if you can borrow that or have it.

    On that WELL page I linked you to, the number is the period of the algorithm. That is, you can get 2^N - 1 numbers before it needs reseeding, where N is: 512, 1024, 19937, or 44497. Mersenne Twister has a period of N = 19937, or 2^19937 - 1. You'll see this is a very large number :)

    The only other thing I can point out is that boost has a random library, which you should find useful.

    In response to your edit, yes the Twister or WELL is that much better than rand(). Also, the old modulus trick harms the distribution of the numbers. Even more reason to use boost :)

    0 讨论(0)
  • 2020-12-02 05:22

    GameRand implement the algorithm posted here http://www.flipcode.com/archives/07-15-2002.shtml

    This is something I originally developed in the late 80s. It easily beat rand() in term of numerical quality, and as the side benefit to be the fastest random algorithm possible.

    0 讨论(0)
  • 2020-12-02 05:28

    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.

    A-ha!

    There's your real requirement!

    No one could fault you for using Mersenne Twister in this application.

    0 讨论(0)
  • 2020-12-02 05:29

    In a real-time game, there's no way for a player to determine the difference between a "good" generator and a "bad" one. In a turn-based game, you're right--some minority of zealots will complain. They'll even tell you stories, in excruciating detail, of how you ruined their lives with a bad random number generator.

    If you need a bunch of genuine random numbers (and you're an online game), you can get some at Random.org. Use them for turn-based games, or as seeds for real-time games.

    0 讨论(0)
提交回复
热议问题