Pergunta

I'm basically trying to build a computer simulation of a casino game idea that uses a standard deck of 52 cards.

I would like to run 1000 games at once.

I've used srand(time(NULL)) before, but I dont think it's possible to output 1000 different random number sequences at one time with this, is there? My perception is that since all the numbers are generated at the same time, that they will all be the same.

Is there a way to use the first generated random number to seed 1000 new 52 number sequences?

Thanks

Foi útil?

Solução 3

Seeding with the same number will always lead to the same sequence of pseudo-random numbers. As long as you don't re-seed with the same seed in between iterations, the (sub)sequences are not going to be the same.

Outras dicas

Yes, you can have your first random number generator (seeded with time(NULL)) output 1000 random numbers, each of which can be used as a seed for 1000 separate random number generators.

My perception is that since all the numbers are generated at the same time, that they will all be the same

They are not generated at the same time. I presume you are using a for loop? Each iteration is not at the same time".

Did you even test this theory?

for i = 1 to 1000 do
{
  print <time to ms resolution>
  print <random number>
}

try seeding the rand() function with srand(time(NULL)*getpid()). PID of the process that you are invoking a 1000 times should be system dependent and has a good chance of being different from one another.

If you are using threads for different games, use threadID instead of getpid().

If you want you can pre-generate all random numbers at once and use them later:

int random_numbers[1000];
for (i = 0; i < 1000; i++) {
    srand(time(NULL)*(i+1));
    random_numbers[i] = rand();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top