C + + random number and rand () defects

Source: Internet
Author: User

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

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.