Home > Article > Backend Development > Is the New Random Library Really Better Than `std::rand()`?
In the famous talk "rand() Considered Harmful," using the engine-distribution paradigm for random number generation is advocated over the classic std::rand() plus modulus simplification.
To test the claims made, an experiment comparing std::rand() with std::mt19937 plus std::uniform_int_distribution, was conducted:
Surprisingly, the aggregate spread of rolls was equal for both methods, suggesting that std::mt19937 plus std::uniform_int_distribution did not provide any additional uniformity. However, the new method was roughly 4x slower.
While this experiment seemed to indicate that std::rand() is not necessarily inferior, it is important to consider the inherent limitations of std::rand():
In contrast, the new
In terms of performance, it is suggested that std::minstd_rand may be a suitable replacement for std::mt19937 when LCG quality is sufficient, potentially offering better speed (particularly when avoiding the distribution adjustment using uniform_int_distribution).
Ultimately, the choice of random number generator depends on the specific requirements of the application, but the new
The above is the detailed content of Is the New Random Library Really Better Than `std::rand()`?. For more information, please follow other related articles on the PHP Chinese website!