How do you write a program that prints 20 random cards?

G

Gary Labowitz

Wojtek said:
There is something wrong with that idea, but just I cannot put my
finger on it :))

Actually, randomly selecting cards from the cards remaining after a
selection has been made and placed in a target list is a more precise
shuffling method. It results in more even distribution of the outcomes. The
process of assigning random numbers to the positions in the deck and then
sorting the random numbers into ascending sequence is another way to
reorganize the cards (i.e. shuffling) and is a good method as well, but
takes longer. This is all covered in Knuth, BTW.
 
W

Wojtek

There are problems with it. It is still a PSEUDO random number
generator. There is a possibility you could greatly narrow down the
odds seeing the first N cards what the last 52-N are.

The second is it could be programmed to deal any hand you wanted. How
would a user know?

Well, that is just it. I used to play the mechanical slot machines, as
the amount of tinkering with the odds was somewhat finite.

The electronic ones that are run by software I just cannot trust. They
are connected to a central controller which monitors every play, and
"I think" modifies the odds on the fly, federal regulators
notwithstanding.
I think it is primarily for the casino. They can trust it more than
they can trust human dealers. It is also faster, allowing more plays
per hour which is inevitably drains the pockets of the players.

Yup, that would make sense.
 
G

George W. Cherry

Roedy Green said:
A newbie begged:

If you can get 20 random numbers 0..51 should be most of the way
there. All you have to do is eliminate dups. Use % to get suit and
number.

see http://mindprod.com/jgloss/randomnumbers.html

Good succinct advice. Remember

4 suits, 13 cards per suit: 4*13 = 52 cards.

Give each card a card number, 0..51.

Give each suit a suit number, 0..3.

Give each card a card value, 0..12.

X/13 gives X's suit.

X%4 gives X's value.

Of course, you have to map 0..3 to clubs..spades

And 0..12 to Ace..King (or maybe deuce..Ace)

It's premature to worry about Java code whilst
you're trying to understand this analysis. The
background analysis is the same for C, Java, C++,
and Lisp.

George W. Cherry
http://sdm.book.home.comcast.net
 
G

George W. Cherry

David said:
I have to write a program that prints 20 random cards (suit,
faceValue). I have to have a driver program and write a constructor
with my own methods. I am fairly new at java, and this concept is
confusing to me at the moment.

Here is a solution from the master of collections himself,
Joshua Bloch.

George

import java.util.*;

class Deal {
public static void main(String args[]) {
// Make a standard 52-card deck
String[] suit = new String[] {"spades", "hearts", "diamonds",
"clubs"};
String[] rank = new String[] {
"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"
};
List deck = new ArrayList();
for (int i = 0; i < suit.length; i++) {
for (int j = 0; j < rank.length; j++) {
deck.add(rank[j] + " of " + suit);
}
}

Collections.shuffle(deck);

for (int j = 0; j < 20; j++) {
System.out.println( deck.get(j) );
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top