public member function
<random>
(1) | explicit mersenne_twister_engine ( result_type val = default_seed ); |
---|
(2) | template <class Sseq>explicit mersenne_twister_engine ( Sseq& q ); |
---|
Construct mersenne twister engine
Constructs a mersenne_twister_engine object, and initializes its internal state sequence to pseudo-random values.
The internal state sequence is seeded either applying a linear random generator on a single value (val) or using a seed sequence object (q).
Parameters
- val
- A seeding value. An entire state sequence is generated from this value using a linear random generator.
result_type is a member type, defined as an alias of the first class template parameter (UIntType).
default_seed is a member constant, defined as 5489u.
- q
- A seed sequence object, such as an object of type seed_seq.
Sseq shall be a seed sequence class, with a generate member function.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// mersenne_twister_engine constructor
#include <iostream>
#include <chrono>
#include <random>
int main ()
{
// obtain a seed from the system clock:
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
// obtain a seed from the user:
std::string str;
std::cout << "Please, enter a seed: ";
std::getline(std::cin,str);
std::seed_seq seed2 (str.begin(),str.end());
std::mt19937 g1 (seed1); // mt19937 is a standard mersenne_twister_engine
std::cout << "A time seed produced: " << g1() << std::endl;
std::mt19937 g2 (seed2);
std::cout << "Your seed produced: " << g2() << std::endl;
return 0;
}
|
Possible output:
Please, enter a seed: Matsumoto and Nishimura
A time seed produced: 749725171
Your seed produced: 4255278487
|
Complexity
Linear on state_size.