So it seems like a lot of people have had similar issues to mine, I could not however find something with exactly the same problem, so if there is an answer already out there feel free to just link that to me.
I am trying to generate a vector of random integers given a size. The problem I am running into is every single element i create is identical. Here is my code:
main {
srand(time(NULL));
int size = 5;
std::vector<int> vec;
for (int i = 0; i < size; i++) {
vec.push_back(rand() - (RAND_MAX / 2));
}
}
when I run this i end up getting a result such as: vec = {123, 123, 123, 123, 123}
or vec = {-352, -352, -352, -352, -352}
(The value changes each time i execute the program, but each loop iteration gives the same value)
Does anyone have any suggestions on what might be causing this? I am only calling srand() once. I have been working on this for a while and suffering some frustration so all help is appreciated.
Aucun commentaire:
Enregistrer un commentaire