So, I've researched this quite a bit and the overwhelming answer I've been seeing for a loop generating the same pseudo-random number is that you're seeing the random number generator inside the loop, but that is not the case with me . . . At least not that I can see.
Basically, as I mentioned, I have a loop "birthing" new bunnies in a loop. And all the new bunnies are being created with the same name and other details.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void BunnyGraduation::breed()
{
for (unsignedint male = 0; male < colony.size(); male++)
{
if (colony.at(male).getSex() == Bunny::Sex::MALE && colony.at(male).getRadioactiveMutantVampireBunny() != true && colony.at(male).getAge() >= AGEOFCONSENT)
{
for (unsignedint female = 0; female < colony.size(); female++)
{
if (colony.at(female).getSex() == Bunny::Sex::FEMALE && colony.at(female).getRadioactiveMutantVampireBunny() != true && colony.at(female).getAge() >= AGEOFCONSENT)
{
birth(colony.at(female).getColor());
}
}
}
}
}
srand(time(0)); is seeded ONCE in the constructor of the bunny object itself. . . Now, come to think of it, the constructor is called every time an object is created . . . And the constructor contains the srand() . . . and the constructor is being called in a loop. . . So therefore, yes, the srand() is being called inside the loop.
BUT now the question becomes. . . How do I fix it?
Normally, srand is called just once, in the beginning of your main() function. Otherwise, because all the structs are created at about the same time (within the same second), the seed will be the same.