How do I make multiple random number generators?

So I've decided to make my first game, but already I've hit a problem.

The code I want to impliment gives the player and the AI a number between 1-1000 each turn.

This is the code I used for the player's turn.

int turn (int followers)
{
int newAmount ;
int k ;

srand((unsigned) time(0));
k=rand()%1000+1;
newAmount = followers + k ;
return newAmount ;
}

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?

Thank you
Did you add the proper libraries?
#include <cstdlib>
#include <ctime>
@Dukaim yes I have

I just want the second AI generator to get it's own numbers and not the same numbers the player's generator gets
Put srand(time(0)); only once in your main(). Maybe that will help?
Last edited on
You can't have two random generators, with separate seeds, using only standard C(++).
Use boost.random : http://www.boost.org/doc/libs/1_44_0/doc/html/boost_random.html
Last edited on
@ Dukaim

do you mean get both the Ai and the player to use the same generator instead of using two functions?
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{

srand(time(0));
}

int function1()
{
k=rand()%1000+1;
}

int function2()
{
k=rand()%1000+1;
}
Last edited on
Oh I see! Sorry, I'm very new with this stuff

Ok I'll try that... I havent heard of this boost technique before, I'll look into that if this doesnt work. Thanks guys.

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.
@ Dukaim

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!)

You get 30 points
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.
That's basically the idea.

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.
Topic archived. No new replies allowed.