jeudi 2 mai 2019

How do you generate a random number correctly in c++? [duplicate]

This question already has an answer here:

So, I am trying to generate a random number so I can get a random position from a vector for my program. But for some reason, although I have done what I researched would work, it still either generates 13 every time or 110 which is completely out of the range for the vector.

I have multiple files, and I read that you are supposed to do srand(time(NULL)) only once in the beginning, so I put it in the beginning of my main file. And then, I used it in a cpp file with variable = rand % 14. I also included stdlib.h and time.h in all the files I'm using rand in.

main:

srand(time(NULL));

cpp file:

num = rand() % 14;

So, I thought this would simply generate a random number between 0 and 14 each time, but instead it just produces either 13 or 110.




Aucun commentaire:

Enregistrer un commentaire