C + + random number

Source: Internet
Author: User

consists of random number distribution class and random number engine class.

We now introduce the random numbers that generate integers and floating-point numbers.

header file <random> The random number engine class used is std::d efault_random_engine, which can be returned by the () call operator with a simple example of a random number:

Main.cpp#include <iostream> #include <random>using std::cout;using std::ends;int Main () {std::d efault_    Random_engine e;    for (int i = 0; i < ++i) {cout << E () << ends; } return 0;}

There is a problem here, the random number returned here is the same, because no seed number is set.

Main.cpp#include <iostream> #include <random> #include <ctime>using std::cout;using std::ends;int Main () {std::d efault_random_engine E (Time (nullptr));//set seed number for (int i = 0; i <; ++i) {cout <    < E () << ends; } return 0;}

A different random number can be generated at this point, if the time difference exceeds 1 seconds before the random number is generated, the random rate is high. The seed number can also be set by E.seed (value), which is the integer type.


If you want to specify a range that can use the random number distribution class Uniform_int_distribution, the class is a template class that has a uniform distribution function:

Main.cpp#include <iostream> #include <random> #include <ctime>using std::cout;using std::ends;int Main () {std::d efault_random_engine E (Time (nullptr));//Set seed number std::uniform_int_distribution<unsigned> U (0,9);  The set type is an unsigned integer, with a range of [0,9] for (int i = 0; i <; ++i) {cout << u (e) << ends; Generates a random number of 0-9} return 0;}

In the following example, the random number is consistent:

 main.cpp#include <iostream> #include  <random> #include  <ctime>using  std::cout;using std::ends;using std::endl;int func () {    std::d efault _random_engine e;     std::uniform_int_distribution<unsigned> u (0,9); The  //  set type is an unsigned integer with a range of [0,9]    for  (int i = 0; i <  10; ++i)     {        cout   << u (e)  << ends;  //  generate 0-9 of random numbers     }     return 0;} Int main () {    for  (int i = 0; i < 10; ++i )     {        func ();         cout << endl;    }    return 0;} 

     because the default_random_engine is updated every time, the distribution class pushes are similar, so the random number is consistent, and the problem can be resolved using a static type:

 main.cpp#include <iostream> #include  <random> #include  <ctime>using  std::cout;using std::ends;using std::endl;int func () {    static  std::d efault_random_engine e; //  data number engine static type     std::uniform_int_distribution <unsigned> u (0,9); //  set type is unsigned integer, the range is [0,9]    for  (int i  = 0; i < 10; ++i)     {         cout  << u (e)  << ends;  //  generate 0-9 of random numbers     }    return 0;} Int main () {    for  (int i = 0; i < 10; ++i )     {        func ();         cout << endl;    }    return 0;} 

This is a static type, so the same random number engine static type is used, and no updates are being pushed in the distribution class at the same time, resulting in a different random number.


In the example above, the integer type is used if the floating-point type double, float, and so on are compiled incorrectly. Generates a floating-point random number, using a random number distribution class uniform_real_distribution, which is a template class that generates a floating-point random number, and generates a compilation error if the integer type unsigned is used:

 main.cpp#include <iostream> #include  <random> #include  <ctime>using  std::cout;using std::ends;using std::endl;int func () {    static  std::d efault_random_engine e; //  data number engine static type     std::uniform_real_ The Distribution<double> u (0,1); //  set type is a double-precision floating-point number with a range of [0,1]    for  (int  i = 0; i < 10; ++i)     {         cout  << u (e)  << ends;  //  Generates a random number of 0-9     }    return 0;} Int main () {    for  (int i = 0; i < 10; ++i )     {        func ();         cout << endl;    }    return 0;} 


Using random number distribution classes and random number engine classes, you can use different functions, such as uniform distribution, normal distribution, and generating random numbers such as integers, floating-point numbers, and so on.


The above is a simple introduction to the random number introduced by c++11 new Standard, welcome to share with us.

This article is from the "zmh009" blog, make sure to keep this source http://zmh009.blog.51cto.com/11619347/1894125

C + + random number

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.