Issue with the rand() function

I'm having an issue generating random numbers using the standard rand() function, which is unfortunate because my entire project depends on random numbers. Can someone tell me why this code generates the same number for each pass of the loop
1
2
3
4
5
6
7
8
for(int i=(max_pop/2)-1; i<max_pop; i++){
		srand(time(0));
		int a, b;
		a=rand()%(max_pop/2);
		b=rand()%(max_pop/2);
		offspring[i]=mate(matingpool[a],matingpool[b]);
		cout << "a: " << a << "   b: " << b << endl;
	}

while this has no problem with it
1
2
3
4
5
for(int i=0;i<5;i++){
		int a;
		a=rand()%10;
		cout << "a: " << a <<endl;
	}

The second section of code produces a unique "a" for every pass of the loop, but for each pass of the loop in the first section "a" and "b" both remain the same. They are different for each run of the program, but the same for each pass of the loop. Any help would be appreciated.
Seed the ranom generator once at the beginning of your program. Your code currently executes in less than a second so of course it seeds it to the same value every single time...
Well then, that was simple enough. Thanks, can't believe I didn't try that.
Topic archived. No new replies allowed.