Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C++ generates random numbers

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report