3

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

chuckieDub
  • 1,767
  • 9
  • 27
  • 46
  • `srand` makes one sequence of random numbers. It only prints the same number when you reseed it or when it happens to generate that number again as the next in the sequence. – chris Dec 12 '12 at 06:41
  • 3
    Considered using C++11's random number generators? – Mark Garcia Dec 12 '12 at 06:41

4 Answers4

1

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.

  • Which, in the end, equals to generating 1000 numbers from the original seed. Setting the pseudorandom seed from pseudorandom generator doesn't really make much sense. – Spook Dec 12 '12 at 07:55
  • As spook says, completely unnecessary. You will get a different, but just as random a result if you don't reseed between iterations. I don't see any reason to use pseudo random numbers as seeds for a pseudo random generator. –  Dec 12 '12 at 12:40
  • What iterations? The OP is wanting 1000 parallel random number generators. –  Dec 12 '12 at 18:46
1

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>
}
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
0

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.

0

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();
}
aakash
  • 751
  • 5
  • 18
  • Relying on system architecture in terms of random number generating doesn't seem to be a good idea. Using current time or tick count as random seed should always be good enough - probability of generating two series with the same seed is a lot smaller than getting the same PID or TID again (especially if OS reuses the IDs, and I believe, it does). – Spook Dec 12 '12 at 08:08
  • @Spook: I am not relying on the system arch entirely. I am using it as an added randomness. Notice the combination of time() and PID(): `srand(time(NULL)*getpid())`. `time(NULL)` is also included. If the process is started at the same time, PID() has to be different. If not, time() will be different. I myself have faced a similar problem of generating random file name where, `srand(time(NULL)); sprintf(outFileName, "%ld%d.tmp", (long)time(NULL), (rand() % 100000));` used to generate a same file name! – aakash Dec 12 '12 at 15:08