PDA

View Full Version : Card Shuffling Algorythm

Mr. Curious
10-24-2005, 02:48 PM
Howdy!

Does anyone have a shuffling algorythm in C/C++ code?

Searching for "Shuffling" didn't turn up anything helpful.

Thanks!

StevieG
10-24-2005, 03:30 PM
From the Perl FAQ on data manipulation, here is the Fisher Yates shuffle, which will shuffle an array in linear time.

This is in Perl, and its randomness is dependent on the rand function.

<font class="small">Code:</font><hr /><pre>
sub fisher_yates_shuffle {
my \$deck = shift; # \$deck is a reference to an array
my \$i = @\$deck;
while (\$i--) {
my \$j = int rand (\$i+1);
@\$deck[\$i,\$j] = @\$deck[\$j,\$i];
}
}
</pre><hr />

Mr. Curious
10-24-2005, 08:16 PM
That code would randomize a deck once, but does it actually emulate a shuffle?

StevieG
10-24-2005, 08:26 PM
Are you actually looking for a stochastic model of physical shuffling techniques like riffling, washing, and boxing?

Mr. Curious
10-25-2005, 11:37 AM
No, I don't think so. What I am looking to do is write a blackjack program that I can use to teach myself basic strategy and then card counting. In order to do it right, I need to create card shuffling algorythm that can completely randomize a deck.

Perhaps there is a better way to represent a deck than to use an array which would then provide for an easy way to shuffle?

StevieG
10-25-2005, 11:51 AM
That shuffle algorithm ought to do the trick.

Mr. Curious
10-25-2005, 12:02 PM
Thanks.

btw - I don't know if Perl allows for the API GetTickCount(), but if so, you can use it as the random seed to ensure that each run is based off of a different random number.

kenberman
10-25-2005, 12:26 PM
[ QUOTE ]
No, I don't think so. What I am looking to do is write a blackjack program that I can use to teach myself basic strategy and then card counting. In order to do it right, I need to create card shuffling algorythm that can completely randomize a deck.

Perhaps there is a better way to represent a deck than to use an array which would then provide for an easy way to shuffle?

[/ QUOTE ]

read about UB's shuffler to know why it is so difficult to do w/ code alone

StevieG
10-25-2005, 12:32 PM
GetTickCount will seed, but see this bit on identical Minesweeper boards. (http://www.techuser.net/mineident1.html)

If you really need to make a good PRNG, it's probably worth looing into some sort of entropy pool for seeding.

I think for your BJ practice app, GetTickCount() would be fine.

cgwahl
10-25-2005, 02:53 PM
[ QUOTE ]
No, I don't think so. What I am looking to do is write a blackjack program that I can use to teach myself basic strategy and then card counting. In order to do it right, I need to create card shuffling algorythm that can completely randomize a deck.

Perhaps there is a better way to represent a deck than to use an array which would then provide for an easy way to shuffle?

[/ QUOTE ]