C + +: Random number generator (Random-number generator) detailed

Source: Internet
Author: User
Tags generator min

Random number, C language function is rand (), C + + is a random number generator (Random-number generator) = Distribution object (distribution object) + engine (engine);

To make a function generate a different random number each time, a static (static) local variable is required so that the distribution object and the engine can hold (hold) state, each time a new one is generated;

Generates a random integer, using the Distributed object Uniform_int_distribution<>, and the default template parameter is int;

Generates a random floating-point number, uses the distributed object Uniform_real_distribution<>, and the default template parameter is double;

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Code:

#include <iostream> #include <vector> #include <string> #include <random> u  
      
Sing namespace std; Std::vector<unsigned> Good_randvec () {static std::d efault_random_engine E;//statically static std::uniform_i nt_distribution<unsigned> u (0,9);  
    Static std::vector<unsigned> ret;  
    For (size_t i=0 i<10; ++i) ret.push_back (U (e));  
return ret;  
    int main () {std::d efault_random_engine E;  
    Std::cout << "Engine:";  
    For (size_t i=0 i<10; ++i) std::cout << E () << "";  
      
    Std::cout << Std::endl;  
    Std::cout << "uniform:"; std::uniform_int_distribution<unsigned> U (0, 9);  
    That is [0, 9] for (size_t i=0 i<10; ++i) std::cout << u (e) << "";  
      
    Std::cout << Std::endl; Std::cout << "min:" << e.min () << "Max:" << E.max () <&lT  
      
    Std::endl;  
    Std::vector<unsigned> v1 (Good_randvec ());  
    Std::vector<unsigned> v2 (Good_randvec ());  
    For (size_t i=0 i<v1.size (); ++i) std::cout << v1[i] << "";  
    Std::cout << Std::endl;  
    For (size_t i=0 i<v2.size (); ++i) std::cout << v2[i] << "";  
      
    Std::cout << Std::endl;  
    Std::uniform_real_distribution<double> UD (0, 1);  
    Std::cout << "float:";  
    For (size_t i=0 i<10; ++i) std::cout << ud (e) << "";  
      
    Std::cout << Std::endl;  
return 0; }

Output:

engine:16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709 Uniform   
: 3 5 8 0 0 5 6 0 3 0 min:1 max:2147483646 0 1 7 4 5 2 0 6 6 9-3 5 8 0 0 5 6 0 3   
float:0.686773 0 .930436 0.526929 0.653919 0.701191 0.762198 0.0474645 0.328234 0.75641 0.365339

Author: csdn Blog spike_king

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.