C++11 Random Number

Source: Internet
Author: User

1 /*************************************************************************2 > File Name:test_random_device.cc3 > Author:sanghai4 > Mail: [email protected]5 > Created time:2017 August 12 Saturday 02:11 04 seconds6  ************************************************************************/7 8 //The standard abstracts random numbers into (random number engines) and (distributes two parts). The engine is used to generate random numbers, distributions that produce a specific distribution of random numbers (such as average distribution, positive distribution, etc.).9 Ten#include <iostream> One#include <random> A using namespacestd; -  - //random number generator (Linux implementation is read/dev/urandom device), usually only as seed (if the hardware is exhausted, Randm_device will fall) the //according to the computer hardware, generate a random number, generally as the seed initialization of the random number engine - voidTest_random_device () - { - Random_device Rd; +      for(inti =0; I < -; ++i) -cout << Rd () <<Endl; + } A  at //pre-defined random number generator, i.e. random number generation algorithm - //Then the seed must, the same algorithm produces the same random sequence, the default seed is 5489u - //based on an algorithm, a random engine (random number Generator) object (that is, random number) that maintains a certain length of non-repeatable sequence is generated. - voidtest_mt19937 () - { - Random_device Rd; in     //***************** Mason Helix Algorithm (), known as the best pseudo-random number generation algorithm default seed:5489u***************** -     //that is, MT is a series generated by the Mason Helix algorithm. to     //32-bit numbers with a state size of 19937 bits + mt19937 Mt (RD ()); -  the     //***************** uses the linear congruence algorithm, the speed is fast, the default seed:1u***************** *     //that is, Mr is a series generated by the linear congruence algorithm. $     //result_type:uint_fast32_t (long int)Panax Notoginseng minstd_rand0 Mr (RD ()); -  the     //result_type:unsinged int + default_random_engine E (rd ()); A     //***************** uses the linear congruence algorithm, the speed is fast, the default seed:1u***************** the } +  - //random number distribution, selecting a range of data distributions from the random number generator object (mt19937 minstd_rand0) $ //results of a certain distribution (statistical probability density) from random numbers $ voidtest_random_distribution () - { -     //generating random integer values I, evenly distributed, i.e., distributed according to the discrete probability function in the closed interval [a, b] the Random_device Rd; - mt19937 Gen (rd ());WuyiUniform_int_distribution<> Disint (1,6); the  -      for(inti =0; I <Ten; ++i) Wucout << Disint (gen) <<','<<Endl; -  AboutUniform_real_distribution<> Disreal (1,2); $  -     //generates random floating-point values I, evenly distributed over intervals [A, b] -      for(inti =0; I <Ten; ++i) -cout << Disreal (gen) <<','; Acout <<Endl; + } the  - intMainvoid) $ { the Test_random_device (); the     return 0; the}
Test_random_device

#include <iostream>#include<vector>#include<string>#include<random>//size not greater than 1000Std::vector<unsigned> Randvec (intSize=Ten,intlow=0,intHigh= +) {Std::cout<<"The number is not greater than 1000"<<Std::endl; //seedsStd::random_device R; //Mason Helix algorithm Data Generator    Staticstd::mt19937 MT (R ()); //[Low, High]    StaticStd::uniform_int_distribution<>dis (low, high); Std::vector<unsigned>ret;  for(inti =0; i < size; ++i) ret.push_back (DIS (MT)); returnret;}//std::vector<unsigned> Instance (Randvec ());
Myranddataengin

Reference:

Cppreference

Cplusplus

Gitbooks

C++11 Random Number

Related Article

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.