Why is this random integer repeating?
Dec 16, 2012 at 3:14am UTC
This is my program:
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 38 39 40 41 42 43 44 45
void roll_strength()
{
srand(time(0));
strength = rand() % (21 - 1) + 1;
}
void roll_constitution()
{
srand(time(0));
constitution = rand() % (21 - 1) + 1;
}
void roll_dexterity()
{
srand(time(0));
dexterity = rand() % (21 - 1) + 1;
}
void roll_intelligence()
{
srand(time(0));
intelligence = rand() % (21 -1) + 1;
}
void roll_wisdom()
{
srand(time(0));
wisdom = rand() % (21 - 1) + 1;
}
void roll_charisma()
{
srand(time(0));
charisma = rand() % (21 - 1) + 1;
}
void roll_values()
{
roll_strength();
roll_constitution();
roll_dexterity();
roll_intelligence();
roll_wisdom();
roll_charisma();
}
It's supposed to spit out different values for each of the stats, but instead it spits out the same number every time. Could I get some suggestions?
Dec 16, 2012 at 3:20am UTC
You should only ever call srand(time(0)); at the start of your program, not in every function. It is just used to start the random number generator it is not directly used.
Dec 16, 2012 at 3:37am UTC
Thank you!
Topic archived. No new replies allowed.