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