dimanche 5 septembre 2021

I am getting same random numbers in c++ even though I am setting seed

I am new to c++ and was trying to create Rock paper scissors and then tried to find how to generate random numbers. So I did a for loop to see if I am getting random numbers but I am not.

Here is the code -

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
    for (int i = 0; i < 5; i++){
        cout<<time(NULL)<<endl;
        srand(time(NULL));
        char arr[] = {'r', 'p', 's'};
        cout<<arr[rand() % 3]<<endl;
    }
    return 0;
}

Output:

1630857527
r
1630857527
r
1630857527
r
1630857527
r
1630857527
r

Any help would be appreciated!




Aucun commentaire:

Enregistrer un commentaire