开发者

C++ Strong Random Number Generator - necessary?

开发者 https://www.devze.com 2023-04-10 10:01 出处:网络
My newest project is the development of a text based RPG and now I need a random number generato开发者_如何学JAVAr, a good one to compute in different situations whether some action can be performed w

My newest project is the development of a text based RPG and now I need a random number generato开发者_如何学JAVAr, 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"?


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.


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.


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.


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.


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消