[C ++ 11] random number generator brought by C ++ 11

Source: Internet
Author: User

C ++ 11 brings many features, and random is one of them.

1. random_device

The standard library providesNon-deterministic Random NumberGenerate a device. in Linux, the device is read/dev/urandom. In Windows, rand_s is used. I strongly condemn it here.

Random_device provides the () operator to return a number between min () and max. in Linux (UNIX like or UNIX), you can use this to generate high-quality random numbers.Real Random Number.

 
# Include <iostream># Include<Random>IntMain () {STD: random_device RD;For(IntN =0; N <20000; ++N) STD: cout<RD () <STD: Endl;Return 0;}

2. Random Number Engine

The random number is abstracted as a random number engine and a random number distribution engine. The engine is used to generate random numbers and distribute random numbers (such as the average distribution and the normal distribution ).

The standard provides three common engines: linear_congruential_engine, mersenne_twister_engine, and subtract_with_carry_engine.AlgorithmThe second is the Mason rotation algorithm, and the third is the linear Coordinator algorithm. the first is the most commonly used, and the speed is also very fast; the second is known as the best pseudo-random number generator; the third is never used ....

The random number engine accepts an integer as a seed. If this parameter is not provided, the default value is used. we recommend that you use random_device to generate a random number as a seed. (In Windows, random_device calls rand_s)

# Include <iostream># Include<Random>IntMain () {STD: random_device RD; STD: mt19937 MT (RD ());For(IntN =0; N <10; N ++) STD: cout<MT () <STD: Endl;Return 0;}

3. Random Number Distributions

The standard provides a variety of distributions, but we often use less, such as the average distribution, which is too distributed... it is also very easy to use.

 //  Average Distribution # Include <random> # Include <Iostream> Int  Main () {STD: random_device RD; STD: mt19937 Gen (RD (); STD: uniform_int_distribution <> DIS ( 1 , 6  );  For ( Int N = 0 ; N < 10 ; ++ N) STD: cout <DIS (Gen) < '   ' ; STD: cout < '  \ N  '  ;} 
 //  Zhengtai Distribution # Include <iostream> # Include <Iomanip> # Include < String > # Include <Map> # Include <Random> # Include <Cmath>Int  Main () {STD: random_device RD; STD: mt19937 Gen (RD ());  //  Values near the mean are the most likely  //  Standard Deviation affects the dispersion of generated values from the mean STD: normal_distribution <> D ( 5 , 2  ); STD: Map < Int , Int > Hist; For ( Int N = 0 ; N < 10000 ; ++ N ){ ++ Hist [STD: round (D (Gen)];}  For  (Auto P: hist) {STD: cout <STD :: Fixed <STD: setprecision ( 1 ) <STD: SETW ( 2  ) <P. First <'   ' <STD :: String (P. Second/ 200 , '  *  ' ) < '  \ N  '  ;}} 

 

Refer:

1. http://en.cppreference.com/w/cpp/numeric/random

2. Windows high quality random number generator, refer to cryptgenrandom API, and http://www.cnblogs.com/egmkang/archive/2011/03/05/1971586.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.