[NBLUG/talk] Request for help--how to program shuffling

Mitch Patenaude mrp at sonic.net
Tue May 20 10:26:02 PDT 2003


On Tuesday, May 20, 2003, at 05:27 US/Pacific, Steve Zimmerman wrote:

> The following program will shuffle a vector of 52 cards--once.  The
> second, third, fourth, etc., times that I run the program, it prints
> out exactly the same "random" shuffle as the first time.
>

Just a guess...  you aren't providing a different seed to the random 
number generator each time you run.    Remember, this a PSEUDOrandrom 
number generator(PRNG).  It will produce exactly the same sequence of 
numbers if it has the same seed.

Which random number generator are you using, rand() or random()?

(I recommend using random() over rand(), it's a much better PRNG, among 
other problems, successive calls to rand() will always alternate 
between even and odd.)

Look at the documentation for random(3)  'type man random',

To get a different seed each runtime, try adding the following line to 
the beginning of main():
	srandom( (int) time(NULL) );

This method of providing a seed isn't cryptographically secure (the 
time your ran the program is too easily guessable, but it's sufficient 
for getting a different seed at each run time.)

   -- Mitch





More information about the talk mailing list