codedude codedude - 4 months ago 38
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?


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