This is supposed to randomly generate the damage dealt by both player and enemy.
It does that, but doesn't generate a new number each time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
srand(time(0));
int damage = (rand() % 150 )+1;
int damage1 = (rand() % 50 )+1;
// This is a pre-check loop that checks the condition before executing it. While the condition is greater than or equal to 0 or 300, it outputs the following.
while(fiend.GetLevel() >=0 && fiend.GetLevel() >= 300)
{
// This calls the function damage, created by the random number generator.
cout << "You hit damage of: " << damage << " with your bare fists!" << endl;
// This decreases the fiends level by the damage generated above.
fiend.Decrease(damage);
// Here, the program gets the new level of the fiend before outputting it to the screen.
cout << "Lady Pearce's health is now: " << fiend.GetLevel() << "\n\n" << endl;
//This calls the second function, damage 1, also created by the random number generator.
cout << "Lady Pearce appears behind you and takes a bite damaging you for " << damage1 << endl;
// This decreases the adventurers level by the damage generated above.
adventurer.Decrease(damage1);
How often does this code run? Specifically, how often does srand(time(0)); get called? If it is more than once during the entire duration of the program, you are using it improperly.
I mean, this is not a complete program. Does this snippet exist in a loop? In a function that gets called many times? If this only runs once, then keskiverto's comment warrants your attention.
We have a nice C++ library called <random>, that allows us to generate random numbers. Why not use it instead of rand? Rand isn't as random as you would probably like.