"C + +" random number engine

Source: Internet
Author: User
Tags rand

RAND ()
  • Basic: When using random numbers, it is often seen that the C standard library provides the function rand (), which generates an integer number between 0 and Rand_max ;
  • Distribution: in order to obtain a random number within a given range, the resulting random number is usually taken out:rand ()%n,rand ()% (n-m) +m;
  • seed: by Srand () sets the random number seed, the seed does not change the case, each time the program runs, calls Rand (), will generate the same sequence of random numbers ;
  • floating point: use double (rand ())/Rand_max to generate a random floating-point number in the range of 0-1, but the accuracy can be problematic;
  • In general, the Srand () seed can be set using time ( 0) and Time () takes the seconds of the system, so if Srand () and rand () call intervals of less than 1s, the same random number is generated, such as:
         for 0 ten; i++)    {        Srand (time (0));         << rand () << Endl;    }

C++11:default_random_engine
  • Basic: C++11 provides a new random number generator, a random number engine default_random_engine, which contains header files when used #include <random>;
  • scope: By default, the build scope of the default_random_engine is a unsigned, which can be obtained from the method min () and Max () .
  • seed: similar to rand (), Default_random_engine also needs to change the resulting sequence by a random number seed, which can be set by invoking the method seed ();
  • distribution and floating point: the random number engine can set the build range by distributing objects,uniform_int_distribution<unsigned> or Uniform_real_distribution <double>;
  • relative rand (), you can use uniform_real_distribution<> to generate random floating-point numbers, and without worrying about the accuracy problem, the random number engine is used as follows:
    Default_random_engine e;//defining the random number engineUniform_int_distribution<unsigned> ID (1,Ten);//Integral type distributionuniform_real_distribution<Double> dd (0,1.0);//floating-point distributionE.seed (Ten);//set random number seed     for(size_t i =0; I <Ten; i++) {cout<< ID (E) <<" ; "<< DD (e) <<Endl; }

"C + +" random number engine

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.