codedude codedude - 1 month ago 5
C++ Question

rand() returns the same number each time the program is run

In this rather basic C++ code snippet involving random number generation:

include <iostream>
using namespace std;

int main() {
cout << (rand() % 100);
return 0;
}


Why am I always getting an output of 41? I'm trying to get it to output some random number between 0 and 100. Maybe I'm not understanding something about how the rand function works?

Answer

You need to "seed" the generator. Check out this short video, it will clear things up.

https://www.thenewboston.com/videos.php?cat=16&video=17503

Comments