codedude codedude - 1 year ago 78
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download