File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Dealing out random, non-repeating cards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dealing out random, non-repeating cards" Watch "Dealing out random, non-repeating cards" New topic

Dealing out random, non-repeating cards

John Walker

Joined: Oct 28, 2003
Posts: 9
I'm in the process of creating a blackjack program and I need for it to deal out cards, but not have them repeat. I was thinking of making each card a variable, but I can't figure out how to make them not repeat themselves.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
There are various ways of doing this, but here's one method. It should work regardless of how you choose to represent your cards.
Step 1: Throw all of your "cards" into an ArrayList. If you choose to represent your cards as 'int' then you'll have to wrap them (in an Integer).
Step 2: Create the holder for the shuffled cards, an ArrayList, or Vector, or array, or however you plan on maintaining the ordered cards.
Step 3: Using "Math.random()" select a random card from the first ArrayList, based on how many cards are remaining. So the the first time you'll be generating a number between 0 and 51. Call the "remove(int)" method on the ArrayList to remove the card from the list and return the card. Put that card into the collection you defined in step two.
Repeat step 3 until the first list is empty. So the second time you do step #3 you'll generate a number between 0 and 50; the third time a number between 0 and 49, etc.
When you finish the first ArrayList will be empty, and the second collection will contain all of the cards in a *random* order.
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

here's your new favorite method (ya just have to store your Card objects as a list in order to use it):

Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood.
The hedge "approximately" is used in the foregoing description because default source of randomenss is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm would choose permutations with perfect uniformity.
This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive.
This method runs in linear time. If the specified list does not implement the RandomAccess interface and is large, this implementation dumps the specified list into an array before shuffling it, and dumps the shuffled array back into the list. This avoids the quadratic behavior that would result from shuffling a "sequential access" list in place.
list - the list to be shuffled.
UnsupportedOperationException - if the specified list or its list-iterator does not support the set method.

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
I agree. Here's the link:
subject: Dealing out random, non-repeating cards
It's not a secret anymore!