Get the App
SLTechnology News&Howtos  ›  Development  › 

How C++ generates random numbers

Shulou Source: shulou.com Published: 2022-06-02 11:56:28 10月30日 Update

This article will explain in detail how C++ generates random numbers. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

C library pseudorandom number generator

Rand

Srand

Most of the time it takes time to generate the seed of the random generator

Int GetRandomNum (int min, int max,int seed)

{

/ / srand ((unsigned) time (NULL)); / / generate seed

Srand (seed)

Return (rand ()% (max-min) + min)

}

Pseudorandom number generator introduced by cymbal 11. Random numbers are abstracted into two parts: random number engine and distribution. The engine is used to generate random numbers, and the distribution produces random numbers with a specific distribution.

The commonly used one is linear uniform distribution.

Uniform_int_distribution

Uniform_real_distribution

Std::random_device rd;// to generate a random number as a seed

Std::uniform_int_distribution uni_dist (0, 9999999); / / Random number generators in a specified range

Std::cout (std::uniform_int_distribution

< unsigned int >

(0, maxRand))

Rand0Engine = make_shared (rd ())

Ranlux48Engine = make_shared (rd ())

}

Std::random_device rd

Std::shared_ptr mtEngine;//32-bit Mersenne Twister by Matsumoto and Nishimura, 1998

Std::shared_ptr mtEngine64; / / 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000 (Matset rotation algorithm)

Std::shared_ptr rand0Engine

Std::shared_ptr ranlux48Engine

Std::shared_ptr > dist

}

Int main ()

{

Cout

Tags: Random numbers generators articles caches engines more good practical portals content that is commonly used applications console articles time normal distribution knowledge seeds programs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia OPPO Reno Apple Docker