The AI's turn is almost identicle, now if I use seed time(0) for both functions then each time I start the program both players will get the same number. If I replace the AI with a seed (such as 1) then he will get a different sequence but the same sequence everytime the program starts. How do I make two random number generators with different sequences each time the program starts?
I'm still new too but I recently had to do a dice roll program and had to use random numbers and the instructor told us to put the srand(time(0)) once in the main because if you put it in each function it will restart it every time, giving the same numbers.
Yes it seems to work, although I'm not sure how random these numbers actually are, I am getting two different sequences each time which is what I wanted (thanks!)
As I understand it, when you call a random number without the srand(time(0)) when the program compiles, it gets a value and every time the program runs it will get the same number.
With srand(time(0)) it initializes it so that you get a different number while the time changes, if you put srand(time(0)) in every function it will nullify the purpose because you will be getting the same number because the time is the same.
random number generators are basically just mathematical formulas.
A simple one might works like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int rand_seed;
void seed(int s)
{
rand_seed = s;
}
int random()
{
rand_seed = (rand_seed * X) + Y;
return rand_seed;
}
// note this is just an example of how one RNG implementation might look. In reality they're
// more complicated than this, and rand/srand certainly behave differently,
// but the concept is the same
X and Y are just constants (typically prime numbers) carefully chosen to produce seemingly random numbers.
When you seed you just set the "starting point" for the formula.