Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Shuffled Poker Deck

Thread view: 
Martin Krainer - 23 Jan 2007 17:12 GMT
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 - 23 Jan 2007 20:54 GMT
> Hi all,
> Im new to Java, and as Training for me I tried to build an Application,
[quoted text clipped - 47 lines]
>     }
> }

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.

Signature

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

Daniel Pitts - 23 Jan 2007 21:27 GMT
> 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
[quoted text clipped - 4 lines]
> 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 - 23 Jan 2007 21:36 GMT
>> (http://www.pokerstars.com/poker/room/features/security/).
> Hmm, There is a problem in their explanation:
[quoted text clipped - 3 lines]
> 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.

Signature

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

Daniel Dyer - 23 Jan 2007 21:41 GMT
>>> (http://www.pokerstars.com/poker/room/features/security/).
>> Hmm, There is a problem in their explanation:
[quoted text clipped - 6 lines]
> 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/f
eatures/security/


They seem to have mislaid their <sup> tag.

Dan.

Signature

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

Oliver Wong - 23 Jan 2007 23:49 GMT
>>>> (http://www.pokerstars.com/poker/room/features/security/).
>>> Hmm, There is a problem in their explanation:
[quoted text clipped - 14 lines]
>
> 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
Daniel Pitts - 23 Jan 2007 21:46 GMT
> >> (http://www.pokerstars.com/poker/room/features/security/).
> > Hmm, There is a problem in their explanation:
[quoted text clipped - 6 lines]
> 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
Lew - 24 Jan 2007 00:36 GMT
> 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
Daniel Pitts - 24 Jan 2007 01:40 GMT
> > 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.
[quoted text clipped - 7 lines]
>
> - 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.
Daniel Pitts - 23 Jan 2007 21:08 GMT
> Hi all,
> Im new to Java, and as Training for me I tried to build an Application,
[quoted text clipped - 4 lines]
> 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();
   }
}
Mustang - 24 Jan 2007 00:52 GMT
Sigh..., why u guys need to implement urselves?
java.util.Collection.shuffle() can handle this ...

On Jan 24, 1:12 am, "Martin Krainer" <NOSPAMmartin.krai...@inode.at>
wrote:
> Hi all,
> Im new to Java, and as Training for me I tried to build an Application,
[quoted text clipped - 45 lines]
>
> }- Hide quoted text -- Show quoted text -
Oliver Wong - 24 Jan 2007 17:45 GMT
> Sigh..., why u guys need to implement urselves?
> java.util.Collection.shuffle() can handle this ...
[quoted text clipped - 4 lines]
>> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.