Random Number repetition with 'for' loops.

Hi this is my first post and after using this website it's helped me out quite a bit and everyone seems very helpful. I was working on some homework and I'm still pretty new to it all. The problem I'm having is that the number it generates is the same number for all the 'stats'. I need each stat to be random but it seems as though they all share the same number based off the first. Can anyone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<vector>
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
	vector<string> stat;
	stat.push_back("Luck");
	stat.push_back("Psych");
	stat.push_back("IQ");
	stat.push_back("Power");
	stat.push_back("Toughness");
	stat.push_back("Nimbleness");
	
	int die[3];
	int total[6];
	
	for (int i=0;i<stat.size();i++)
	{
		for (int j=0;j<3;j++)
		{
			srand(static_cast<unsigned int>(time(0)));
			die[j]=(rand() % 6)+1;
		}
		total[i]=die[0]+die[1]+die[2];
	}
	
	for(int i=0;i<stat.size();i++)
	{
		cout<<"Your "<<stat[i]<<" is "<<total[i]<<".\n";
	}
	
	return 0;
	
}
You should only seed the random number generator once. Just move
srand(static_cast<unsigned int>(time(0)));
outside the loop. Putting it at the start of main is usually best.
It worked! Thank you so much! Makes sense too haha. Thank you again, much appreciated.
Topic archived. No new replies allowed.