how to create random numbers in c++ code example

Example: random numbers c++

/*The problem with srand(time(NULL)) and rand() is that if you use them
in a loop it'll probably be executed during the same clock period
and therefore rand() will return the same number. To solve this
you can use the library random to help you.*/

#include <random>

std::random_device rd;
std::mt19937 e{rd()};
std::uniform_int_distribution<int> dist{1, 5}; //Limits of the interval
//Returns a random number between {1, 5} with
dist(e);

Tags:

Cpp Example