Home  >  Article  >  Backend Development  >  Is the New Random Library Really Better Than `std::rand()`?

Is the New Random Library Really Better Than `std::rand()`?

Susan Sarandon
Susan SarandonOriginal
2024-10-31 07:03:30467browse

Is the New Random Library Really Better Than `std::rand()`?

Why is the New Random Library 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:

  1. Two functions, getRandNum_Old and getRandNum_New, were devised for generating random numbers between 0 and 5 using the two methods.
  2. 960,000 random numbers were generated using both approaches, and the frequencies of the numbers 0-5 were tallied.
  3. The standard deviations of the frequencies were computed for 1000 simulations.
  4. The time taken for both methods was also recorded.

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():

  • It uses a linear congruential generator (LCG), which is known to exhibit certain weaknesses such as low randomness of low-order bits, short period, and limited RAND_MAX.
  • The srand interface limits the number of starting seeds (and thus generated sequences) due to the fact that it takes an unsigned int.

In contrast, the new header in the C Standard Library:

  • Provides fully specified algorithms with guaranteed characteristics.
  • Encapsulates generators in classes, eliminating global state and related problems.
  • Includes a default random_device for seeding.
  • Allows for the implementation of custom generators if desired.

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 library provides several advantages over the legacy std::rand() implementation, particularly for applications that demand high-quality random numbers.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn