[C++] rand() is giving me the same number over and over | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[C++] rand() is giving me the same number over and over

I need help, every time I run this code: randomNumber = 1+(rand()%3); cout << randomNumber; The output in the console every time I run the program is always 3, but it's supposed to be a random number between 1 and 3.

14th Apr 2017, 12:35 AM
Joel C.
Joel C. - avatar
2 Answers
+ 11
You did not seed it first with srand(time(0)); include <time.h> to use it.
14th Apr 2017, 12:39 AM
Karl T.
Karl T. - avatar
+ 1
Thanks for the help.
14th Apr 2017, 4:00 AM
Joel C.
Joel C. - avatar