Possible Duplicate:
How often should I call srand() in a C++ application?
I am trying to implement rolling multiple dice but with my code, even if i create 2+ dice, they always seem to roll the same number. Here's my code:
Dice::Dice() {}
void Dice::roll()
{
srand(time(0));
setFaceValue((rand() % MAX_FACE_VALUE + 1));
}
int Dice::getFaceValue()
{
return currentFaceValue;
}
void Dice::setFaceValue(int value)
{
currentFaceValue = value;
}
If I program this into my driver, i get the same number 3 times.
int main()
{
Dice d1, d2, d3;
d1.roll();
cout << d1.getFaceValue() << endl;
d2.roll();
cout << d2.getFaceValue() << endl;
d3.roll();
cout << d3.getFaceValue() << endl;
}