C + + generates random integers and floating-point numbers as follows:
#include <random>using namespacestd;int_tmain (intARGC, _tchar*argv[]) {cout<<"random number test start:"<<Endl; Default_random_engine Dre; Uniform_int_distribution<int> di (Ten, -); for(intI=0;i<Ten; i++) {cout<< di (DRE) <<" "; } cout<<Endl; Uniform_real_distribution<Double> Dr (Ten, -); for(intI=0;i<Ten; i++) {cout<< Dr (Dre) <<" "; } cout<<Endl; Vector<int>vs; Vs.push_back (1); Vs.push_back (2); Vs.push_back (3); Vs.push_back (4); Vs.push_back (5); Vs.push_back (6); Vs.push_back (7); Vs.push_back (8); Vs.push_back (9); Vs.push_back (0); Shuffle (Vs.begin (), Vs.end (), Dre); for(inti =0; I<vs.size (); i++) {cout<< Vs[i] <<" "; } cout<<Endl; cout<<"random number test ended."<<Endl;}
The output is as follows:
Random number Test start: 16 13 20 19 14 17 10 16 15 1410.9754 15.4722 12.785 11.8838 15.4688 19.9288 19.5751 19.9646 19.6489 19.67690 3 2 9 5 1 6 7 4 8 random number test ended.
You can also use Rand of standard C, as follows:
#include <cstdlib>int main () { //Srand (ten); for (int0; i + +) " " // returns between 0~32767 cout << Endl;}
The output is as follows:
41 18467 6334 26500 19169 15724 11478 29358 26962 24464
On the question of using the random number engine or the rand () function, the second edition of the C + + standard library 17.1.1 see the following description:
C + + random number and rand () defects