mardi 24 mars 2015

C++ random number generator called multiple times [duplicate]


This question already has an answer here:




Hi I'm having trouble with my random number generator giving me the same sequence (sequence is different though) of numbers every time I run it. Here's a simplified tidbit of code to demonstrate my problem.



#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int random(){
srand(time(0));
return(1+rand() % 6);
}
int main(){
cout<< random() <<endl;
cout<< random() <<endl;
} // will return a repeated double digit i.e. 11, 22, 33, 44




Aucun commentaire:

Enregistrer un commentaire