Shuffled Poker Deck  
Author Message
Martin Krainer





PostPosted: 2007-1-24 1:13:00 Top

java-programmer, Shuffled Poker Deck Hi all,
Im new to Java, and as Training for me I tried to build an Application,
which returns a shuffled Pokerdeck. (Random from 1 to 52)
Maybe you will laugh, but i needed a whole day to solve it, and I think its
even not good solution.
Well, at least it works, but please could you give me a hint, how to solve
this problem easier.
Heres the code:
/**
* @(#)Poker.java
*
*
* @author Martin Krainer
* @version 1.00 2007/1/22
*/

public class Poker {

static int[] deck = new int[52];

void buildDeck() { // builds a deck with 52
(hopefully) different Integers
for (int i=0; i<52; i++) {
deck[i] = (int)(Math.random()*10E7);
}
}

void shuffledDeck(int[] a) { // now here I tried hard and
long to get a field with numbers from 1 to 52
long[] zahl= new long[52];
for (int i=0; i<10E7; i++) {
for (int j=0; j<52; j++) {
if (i == deck[j]) {
zahl[j] = j;
System.out.print( " " + (zahl[j]+1) );
}
}
}
}

public static void main(String[] args) {

Poker p = new Poker();
p.buildDeck();
p.shuffledDeck(deck); //well, it works, but...
what do you think?
}
}



 
Daniel Dyer





PostPosted: 2007-1-24 4:55:00 Top

java-programmer >> Shuffled Poker Deck On Tue, 23 Jan 2007 17:12:39 -0000, Martin Krainer
<email***@***.com> wrote:

> Hi all,
> Im new to Java, and as Training for me I tried to build an Application,
> which returns a shuffled Pokerdeck. (Random from 1 to 52)
> Maybe you will laugh, but i needed a whole day to solve it, and I think
> its
> even not good solution.
> Well, at least it works, but please could you give me a hint, how to
> solve
> this problem easier.
> Heres the code:
> /**
> * @(#)Poker.java
> *
> *
> * @author Martin Krainer
> * @version 1.00 2007/1/22
> */
>
> public class Poker {
>
> static int[] deck = new int[52];
>
> void buildDeck() { // builds a deck with 52
> (hopefully) different Integers
> for (int i=0; i<52; i++) {
> deck[i] = (int)(Math.random()*10E7);
> }
> }
>
> void shuffledDeck(int[] a) { // now here I tried hard
> and
> long to get a field with numbers from 1 to 52
> long[] zahl= new long[52];
> for (int i=0; i<10E7; i++) {
> for (int j=0; j<52; j++) {
> if (i == deck[j]) {
> zahl[j] = j;
> System.out.print( " " + (zahl[j]+1) );
> }
> }
> }
> }
>
> public static void main(String[] args) {
>
> Poker p = new Poker();
> p.buildDeck();
> p.shuffledDeck(deck); //well, it works, but....
> what do you think?
> }
> }

You only need to initialise your deck with the numbers between 0 and 51,
you can do this without random numbers. Another option is to use an
enumerated type to model the playing cards. Either way, all your
buildDeck method needs to do is to ensure that each card is different, it
doesn't matter if they are all in order at this point, you will shuffle
them later.

Don't use Math.random to generate random integers, use java.util.Random
instead. Roedy Green has a decent discussion of generating random numbers
in Java (here http://mindprod.com/jgloss/pseudorandom.html).

Which is more important to you here, the means or the end? If you just
want to shuffle a deck as easily as possible, take a look at the shuffle
method in the java.util.Collections class. If you want to write the
shuffling routine yourself, search for the Fisher-Yates algorithm and try
implementing that (I believe that the java.util.Collections shuffle method
is an implementation of this algorithm). Alternatively, try the trivial
algorithm used by PokerStars and described on their site
(http://www.pokerstars.com/poker/room/features/security/).

Dan.

--
Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
 
Daniel Pitts





PostPosted: 2007-1-24 5:09:00 Top

java-programmer >> Shuffled Poker Deck
Martin Krainer wrote:
> Hi all,
> Im new to Java, and as Training for me I tried to build an Application,
> which returns a shuffled Pokerdeck. (Random from 1 to 52)
> Maybe you will laugh, but i needed a whole day to solve it, and I think its
> even not good solution.
> Well, at least it works, but please could you give me a hint, how to solve
> this problem easier.
> Heres the code:
[snip the code]

Well, it doesn't look all that great to me.
Here's one that looks better.


class Deck {
final java.util.List<Card> cards;
private static final int DECK_SIZE = Card.NUMBER_OF_CARD_TYPES;

public Deck() {
this.cards = new java.util.ArrayList<Card>();
for (int i = 0; i < DECK_SIZE; ++i) {
cards.add(Card.forOrdinal(i));
}
}

public void shuffle() {
java.util.Collections.shuffle(cards);
}

public void print() {
for (Card card: cards) {
System.out.print(" '" + card + "' ");
}
System.out.println();
}
}

class Card {
private static final int NUMBER_OF_VALUES = Value.values().length;
private static final int NUMBER_OF_SUITS = Suit.values().length;
public static final int NUMBER_OF_CARD_TYPES =
NUMBER_OF_SUITS*NUMBER_OF_VALUES;

enum Suit {
diamonds, clubs, hearts, spades;
private static Suit forOrdinal(int ordinal) {
return values()[ordinal / NUMBER_OF_VALUES];
}
}
enum Value {
ace, two, three, four, five, six, seven, eight, nine, ten,
jack, queen, king;

private static Value forOrdinal(int ordinal) {
return values()[ordinal];
}
}
final Suit suit;
final Value value;
private Card(Suit suit, Value value) {
this.suit = suit;
this.value = value;
}
public static Card forOrdinal(int ordinal) {
return new Card(Suit.forOrdinal(ordinal),
Value.forOrdinal(ordinal));
}

public String toString() {
return value.toString() + " of " + suit.toString();
}
}

public class Poker {
public static void main(String[] args) {
Deck deck = new Deck();
deck.print();
deck.shuffle();
deck.print();
}
}

 
 
Daniel Pitts





PostPosted: 2007-1-24 5:27:00 Top

java-programmer >> Shuffled Poker Deck
Daniel Dyer wrote:
> Which is more important to you here, the means or the end? If you just
> want to shuffle a deck as easily as possible, take a look at the shuffle
> method in the java.util.Collections class. If you want to write the
> shuffling routine yourself, search for the Fisher-Yates algorithm and try
> implementing that (I believe that the java.util.Collections shuffle method
> is an implementation of this algorithm). Alternatively, try the trivial
> algorithm used by PokerStars and described on their site
> (http://www.pokerstars.com/poker/room/features/security/).
Hmm, There is a problem in their explanation:
"A deck of 52 cards can be shuffled in 52! ways. 52! is about 2225. We
use 249 random bits from both entropy sources (user input and thermal
noise) to achieve an even and unpredictable statistical distribution."
They are right about the 52! ways, but 52! is around 1.55e66, not 2225
The number of bits required to store that is 220, Ohwell.

 
 
Daniel Dyer





PostPosted: 2007-1-24 5:36:00 Top

java-programmer >> Shuffled Poker Deck On Tue, 23 Jan 2007 21:27:28 -0000, Daniel Pitts
<email***@***.com> wrote:
>
> Daniel Dyer wrote:
>> (http://www.pokerstars.com/poker/room/features/security/).
> Hmm, There is a problem in their explanation:
> "A deck of 52 cards can be shuffled in 52! ways. 52! is about 2225. We
> use 249 random bits from both entropy sources (user input and thermal
> noise) to achieve an even and unpredictable statistical distribution."
> They are right about the 52! ways, but 52! is around 1.55e66, not 2225
> The number of bits required to store that is 220, Ohwell.

I think that must be a typo. I'm certain I read that page before and it
said 225. I've done the same calculations previously and got 226.

Dan.

--
Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
 
 
Daniel Dyer





PostPosted: 2007-1-24 5:41:00 Top

java-programmer >> Shuffled Poker Deck On Tue, 23 Jan 2007 21:36:11 -0000, Daniel Dyer <"You don't need it">
wrote:

> On Tue, 23 Jan 2007 21:27:28 -0000, Daniel Pitts
> <email***@***.com> wrote:
>>
>> Daniel Dyer wrote:
>>> (http://www.pokerstars.com/poker/room/features/security/).
>> Hmm, There is a problem in their explanation:
>> "A deck of 52 cards can be shuffled in 52! ways. 52! is about 2225. We
>> use 249 random bits from both entropy sources (user input and thermal
>> noise) to achieve an even and unpredictable statistical distribution."
>> They are right about the 52! ways, but 52! is around 1.55e66, not 2225
>> The number of bits required to store that is 220, Ohwell.
>
> I think that must be a typo. I'm certain I read that page before and it
> said 225. I've done the same calculations previously and got 226.
>

Yep,

http://web.archive.org/web/20060505032523/http://www.pokerstars.com/poker/room/features/security/

They seem to have mislaid their <sup> tag.

Dan.

--
Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
 
 
Daniel Pitts





PostPosted: 2007-1-24 5:47:00 Top

java-programmer >> Shuffled Poker Deck
Daniel Dyer wrote:
> On Tue, 23 Jan 2007 21:27:28 -0000, Daniel Pitts
> <email***@***.com> wrote:
> >
> > Daniel Dyer wrote:
> >> (http://www.pokerstars.com/poker/room/features/security/).
> > Hmm, There is a problem in their explanation:
> > "A deck of 52 cards can be shuffled in 52! ways. 52! is about 2225. We
> > use 249 random bits from both entropy sources (user input and thermal
> > noise) to achieve an even and unpredictable statistical distribution."
> > They are right about the 52! ways, but 52! is around 1.55e66, not 2225
> > The number of bits required to store that is 220, Ohwell.
>
> I think that must be a typo. I'm certain I read that page before and it
> said 225. I've done the same calculations previously and got 226.

Okay, I see three problems.
First, my mistake: I calculated 51!, not 52!
Second, their typo. They should ceil that value anyway, so it would be
226
Third: The're lack of units.
The statement probably should be:
"All values for 52! can be represented in 226 bits"

Oh, and 52! is around 8.066e67, python lets you calculate it exactly:
echo "print reduce(lambda x,y: x*y, xrange(1, 53))" | python

 
 
Oliver Wong





PostPosted: 2007-1-24 7:50:00 Top

java-programmer >> Shuffled Poker Deck
"Daniel Dyer" <"You don't need it"> wrote in message
news:email***@***.com...
> On Tue, 23 Jan 2007 21:36:11 -0000, Daniel Dyer <"You don't need it">
> wrote:
>
>> On Tue, 23 Jan 2007 21:27:28 -0000, Daniel Pitts
>> <email***@***.com> wrote:
>>>
>>> Daniel Dyer wrote:
>>>> (http://www.pokerstars.com/poker/room/features/security/).
>>> Hmm, There is a problem in their explanation:
>>> "A deck of 52 cards can be shuffled in 52! ways. 52! is about 2225. We
>>> use 249 random bits from both entropy sources (user input and thermal
>>> noise) to achieve an even and unpredictable statistical distribution."
>>> They are right about the 52! ways, but 52! is around 1.55e66, not 2225
>>> The number of bits required to store that is 220, Ohwell.
>>
>> I think that must be a typo. I'm certain I read that page before and it
>> said 225. I've done the same calculations previously and got 226.
>>
>
> Yep,
>
> http://web.archive.org/web/20060505032523/http://www.pokerstars.com/poker/room/features/security/
>
> They seem to have mislaid their <sup> tag.
>
> Dan.

The problems with the explanation doesn't really affect the shuffling
algorithm, but if security issues are interesting to you, note that they use
SHA-1 which has been broken a couple of years ago:
http://www.schneier.com/blog/archives/2005/02/cryptanalysis_o.html

- Oliver


 
 
Lew





PostPosted: 2007-1-24 8:37:00 Top

java-programmer >> Shuffled Poker Deck Daniel Pitts wrote:
> Oh, and 52! is around 8.066e67, python lets you calculate it exactly:
> echo "print reduce(lambda x,y: x*y, xrange(1, 53))" | python

and nearly instantaneously on my machine. Phew.

80658175170943878571660636856403766975289505440883277824000000000000

Blink of an eye - is it python that makes it so fast?

(And kudos to Fedora for including python - I didn't even know I had it but
tried on a guess.)

- Lew
 
 
Mustang





PostPosted: 2007-1-24 8:53:00 Top

java-programmer >> Shuffled Poker Deck Sigh..., why u guys need to implement urselves?
java.util.Collection.shuffle() can handle this ...

On Jan 24, 1:12 am, "Martin Krainer" <email***@***.com>
wrote:
> Hi all,
> Im new to Java, and as Training for me I tried to build an Application,
> which returns a shuffled Pokerdeck. (Random from 1 to 52)
> Maybe you will laugh, but i needed a whole day to solve it, and I think its
> even not good solution.
> Well, at least it works, but please could you give me a hint, how to solve
> this problem easier.
> Heres the code:
> /**
> * @(#)Poker.java
> *
> *
> * @author Martin Krainer
> * @version 1.00 2007/1/22
> */
>
> public class Poker {
>
> static int[] deck = new int[52];
>
> void buildDeck() { // builds a deck with 52
> (hopefully) different Integers
> for (int i=0; i<52; i++) {
> deck[i] = (int)(Math.random()*10E7);
> }
> }
>
> void shuffledDeck(int[] a) { // now here I tried hard and
> long to get a field with numbers from 1 to 52
> long[] zahl= new long[52];
> for (int i=0; i<10E7; i++) {
> for (int j=0; j<52; j++) {
> if (i == deck[j]) {
> zahl[j] = j;
> System.out.print( " " + (zahl[j]+1) );
> }
> }
> }
> }
>
> public static void main(String[] args) {
>
> Poker p = new Poker();
> p.buildDeck();
> p.shuffledDeck(deck); //well, it works, but...
> what do you think?
> }
>
>
>
> }- Hide quoted text -- Show quoted text -

 
 
Daniel Pitts





PostPosted: 2007-1-24 9:40:00 Top

java-programmer >> Shuffled Poker Deck On Jan 23, 4:36 pm, Lew <email***@***.com> wrote:
> Daniel Pitts wrote:
> > Oh, and 52! is around 8.066e67, python lets you calculate it exactly:
> > echo "print reduce(lambda x,y: x*y, xrange(1, 53))" | pythonand nearly instantaneously on my machine. Phew.
>
> 80658175170943878571660636856403766975289505440883277824000000000000
>
> Blink of an eye - is it python that makes it so fast?
>
> (And kudos to Fedora for including python - I didn't even know I had it but
> tried on a guess.)
>
> - Lew
I think python is somewhat standard in many linux distro's now.

Anyway, its really not that expensive of an operation, its just 52
multiplies. They use big integer arithmatic, but that doesn't add that
much complexity. Its basically an o(log(m)*log(n)) operation to
multiple m and n. I think the base on the log is probably 2^64 or
2^32, so its pretty small time for 52!
500! is even fast to calculate, it starts to get slower above that.
Mostly, it takes more time to print it than to calculate it.

 
 
Oliver Wong





PostPosted: 2007-1-25 1:46:00 Top

java-programmer >> Shuffled Poker Deck
"Mustang" <email***@***.com> wrote in message
news:email***@***.com...
> Sigh..., why u guys need to implement urselves?
> java.util.Collection.shuffle() can handle this ...
>
> On Jan 24, 1:12 am, "Martin Krainer" <email***@***.com>
> wrote:
>> Hi all,
>> Im new to Java, and as Training for me I tried to build an Application,
>> which returns a shuffled Pokerdeck. (Random from 1 to 52)

As stated in the original post, the author tried to implement it himself
as a form of training.

- Oliver