I have a problem with the following code that I have written in C++. I need this kind of function in some other program. The problem is I am getting the same random numbers when I call the function multiple times. I don't know why this is happening and is there any way to get it fixed.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
using namespace std;
int new_random()
{
srand((unsigned)time(NULL));
int r{rand()};
return r;
}
int main()
{
int a{new_random()};
int b{new_random()};
cout << a << endl
<< b << endl;
}
In every run, I am getting a and b to be same.
I will highly appreciate if somebody can show me some light to get two different random numbers in this way. Thanks a lot.
Aucun commentaire:
Enregistrer un commentaire