Skip to content

Instantly share code, notes, and snippets.

Created November 2, 2019 21:22
Show Gist options
  • Save zhangxiaomu01/8545cc7d7968323adce9b9075bbb632a to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/8545cc7d7968323adce9b9075bbb632a to your computer and use it in GitHub Desktop.
using namespace std;
void printRandom(default_random_engine e){
for(int i = 0; i < 10; i++)
cout << e() << " ";
cout << endl;
int main(){
//Define the default random engine.
default_random_engine eng;
//generate two random numbers
cout << "random number 1:" << eng() <<endl;
cout << "random number 2:" << eng() <<endl;
//output the min and max values of default random engine. The two values
//define the range.
cout << "min value: " << eng.min() <<endl;
cout << "max value: " << eng.max() <<endl;
//Random engine has an internal state which determines which number
//it should generate. The engine with the same state will always produce
//the same random number.
stringstream state;
state << eng; //save the current state of the engine
cout << "random number 3:" << eng() <<endl;
cout << "random number 4:" << eng() <<endl;
state >> eng; //restore the state we have saved.
//Now the number 5 and 6 will be exactly the same as number 3 and 4
cout << "random number 5:" << eng() <<endl;
cout << "random number 6:" << eng() <<endl;
//All the default engine will produce the same value;
default_random_engine e;
default_random_engine e1;
//The following code will produce exactly the same result!
//3499211612 581869302 3890346734 3586334585 545404204 4161255391 3922919429
//949333985 2715962298 1323567403
//We typically use chrono library method as our seed. Then each time when we
//run our program, we are guaranteed to have a distinct set of random numbers.
//The following function gent the total number of counts since the epoch
//of computer time.
//(00:00 January 1st, 1970 (coordinated Universal Time – UTC))
unsigned int seed = chrono::steady_clock::now().time_since_epoch().count();
//with seed, the random engine will be able to generate different
//sets of random numbers! It determines the internal state of the engine
default_random_engine e3(seed);
//seed() method can change the state of default random engine.
e.seed(); //set engine to initial state
e.seed(109); //set engine to a state to seed 109
//Random engine can also be applied to shuffle a vector or deque.
vector<int> d = {1, 2, 3, 4, 5, 6, 7, 8, 9};
shuffle(d.begin(), d.end(), default_random_engine());
for (int num : d) {
cout << num << " ";
cout << endl;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment