T
Thomas Korsgaard
Hello,
I have a question conserning generation of random numbers in java. I'm
developing a little cardgame. In order to do so i have created a class
Deck which contains of 52 Card objects (which I alsp have created).
These "Cards" are stored in an Card array. I want to be able to
"shuffle" the deck, and in order to do so I have created shuffle(),
whitch takes a card from the deck an puts it in the back:
Random r1 = new Random();
void shuffle(){
for(int i=0;i<4000;i++){
int pos = r1.nextInt(52);
Card temp = d1[pos];
this.getCard(pos);
this.putCard(temp);
}
}
This works fine! No problems. The problem comes when I start testing
this on loads many decks. If I run a test like:
Deck lots[] = new Deck[20];
for(int i=0;i<lots.length;i++) {
// Init deck
lots = new Deck();
// Shuffel the deck
lots.shuffle();
// What are the ten top cards
for(int j=0;j<10;j++){
lots.showCard(j).printCard();
}
System.out.println("");
}
And the output is as follows:
7D 5H 8D 9H KD KC QS 7S 3D 2D
8H JH QC 2S 8C 2D 5D 5C KC 2H
8H JH QC 2S 8C 2D 5D 5C KC 2H
7H KS 2D 6S 8S 2H 6H 2C QH 4D
7H KS 2D 6S 8S 2H 6H 2C QH 4D
6H 8C 4S 2C JS AC 3S 10H 3C KH
6H 8C 4S 2C JS AC 3S 10H 3C KH
6H 8C 4S 2C JS AC 3S 10H 3C KH
6H 8C 4S 2C JS AC 3S 10H 3C KH
QD 10S QS 3H 9S 2H 8D QH 7S 3C
QD 10S QS 3H 9S 2H 8D QH 7S 3C
QD 10S QS 3H 9S 2H 8D QH 7S 3C
QD 10S QS 3H 9S 2H 8D QH 7S 3C
4S AS 5D QD 2D 7H 9D 3S 5C 10C
4S AS 5D QD 2D 7H 9D 3S 5C 10C
JS 8C 7S KD 6D 2H 8S QD 9C 3H
8H 6D QD 5H QC 6C 10C 10H 3S AH
9S 3H 3C KS 6D QS 8S JD 3D 6H
9S 3H 3C KS 6D QS 8S JD 3D 6H
9S 3H 3C KS 6D QS 8S JD 3D 6H
As you can see it shuffels some deck the same way! And I think this is
due to my random generator ...
Can anybody help me?
Cheers,
/Thomas
I have a question conserning generation of random numbers in java. I'm
developing a little cardgame. In order to do so i have created a class
Deck which contains of 52 Card objects (which I alsp have created).
These "Cards" are stored in an Card array. I want to be able to
"shuffle" the deck, and in order to do so I have created shuffle(),
whitch takes a card from the deck an puts it in the back:
Random r1 = new Random();
void shuffle(){
for(int i=0;i<4000;i++){
int pos = r1.nextInt(52);
Card temp = d1[pos];
this.getCard(pos);
this.putCard(temp);
}
}
This works fine! No problems. The problem comes when I start testing
this on loads many decks. If I run a test like:
Deck lots[] = new Deck[20];
for(int i=0;i<lots.length;i++) {
// Init deck
lots = new Deck();
// Shuffel the deck
lots.shuffle();
// What are the ten top cards
for(int j=0;j<10;j++){
lots.showCard(j).printCard();
}
System.out.println("");
}
And the output is as follows:
7D 5H 8D 9H KD KC QS 7S 3D 2D
8H JH QC 2S 8C 2D 5D 5C KC 2H
8H JH QC 2S 8C 2D 5D 5C KC 2H
7H KS 2D 6S 8S 2H 6H 2C QH 4D
7H KS 2D 6S 8S 2H 6H 2C QH 4D
6H 8C 4S 2C JS AC 3S 10H 3C KH
6H 8C 4S 2C JS AC 3S 10H 3C KH
6H 8C 4S 2C JS AC 3S 10H 3C KH
6H 8C 4S 2C JS AC 3S 10H 3C KH
QD 10S QS 3H 9S 2H 8D QH 7S 3C
QD 10S QS 3H 9S 2H 8D QH 7S 3C
QD 10S QS 3H 9S 2H 8D QH 7S 3C
QD 10S QS 3H 9S 2H 8D QH 7S 3C
4S AS 5D QD 2D 7H 9D 3S 5C 10C
4S AS 5D QD 2D 7H 9D 3S 5C 10C
JS 8C 7S KD 6D 2H 8S QD 9C 3H
8H 6D QD 5H QC 6C 10C 10H 3S AH
9S 3H 3C KS 6D QS 8S JD 3D 6H
9S 3H 3C KS 6D QS 8S JD 3D 6H
9S 3H 3C KS 6D QS 8S JD 3D 6H
As you can see it shuffels some deck the same way! And I think this is
due to my random generator ...
Can anybody help me?
Cheers,
/Thomas