Random number generator

Anyone good with random number generators?

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.
What do you mean by "each time"? Your code creates exactly two values, because you call rand() only two times.
What do you mean "How often does this code run?"
srand(time(0)); gets called once as far as I can see.
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.
srand(time(NULL));
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.
MatthewRock


How would I call such a thing?
You start by reading its documentation:
http://www.cplusplus.com/reference/random/

just like you have read the docs of rand()
http://www.cplusplus.com/reference/cstdlib/rand/
Last edited on
Topic archived. No new replies allowed.