mardi 1 décembre 2020

Random string generator in c++ returns the same value in loops

I have a function that generates random string in my program and that works as I want, but only separately from loops. When I use it in loop in main(), it returns the same string every time (let's say 20 for example). Here is that function:

string randomString()
{
string c;
int r = NULL;
int num = 7;
srand(time(NULL));
for (int i = 0; i < num; i++)
{
    r = rand() % 26;
    c += 'a' + r;   
}
cout << "Random string: " << c << " \n";
return c;
}

What should I do to make it return different string every loop? Thanks for any help.




Aucun commentaire:

Enregistrer un commentaire