Random Card Shuffle

Difficulties with storing random sorthing.

Page 1 of 1

6 Replies - 5551 Views - Last Post: 22 September 2010 - 04:39 PM Rate Topic: -----

#1 eiarzate  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 33
  • Joined: 21-January 10

Random Card Shuffle

Posted 21 September 2010 - 05:20 AM

I'm having a hard time sorting the random array for shuffling cards. Here is my code.
Any help is appreciated thank you.
public void shuffle() {

        boolean check[] = new boolean[52];

        for (int i = 0; i < check.length; i++) {
            check[i] = false;
        }


        for (int i = 0; i < cards.length; i++) {

            int random = (int) Math.random() * 52;

            if (check[random] == true) {

                random = (int) Math.random() * 52;

            }

            check[random] = true;

            System.out.println(cards[random].getSuit() + " " + cards[random].getValue());
            shuffledCards[random].setSuit(cards[random].getSuit());
            shuffledCards[random].setValue(cards[random].getValue());

        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Random Card Shuffle

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Random Card Shuffle

Posted 21 September 2010 - 06:12 AM

Perhaps it would be easier to utilize the Collections.shuffle() method by converting your array to a List and back:
List<Integer> list = Arrays.asList(new Integer[]{1,2,3,4,5});
Collections.shuffle(list);
Integer[] x = list.toArray(new Integer[list.size()]);


Was This Post Helpful? 0
  • +
  • -

#3 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Random Card Shuffle

Posted 21 September 2010 - 09:45 AM

macosxnerd offers a nice, clean solution. However if you want, you could also use a Random number generator from the Random class to generate a number between 0 and 51 which are your array bounds. For each iteration, swap the value of the array at the loop variable with the value of the array at the random number:

swap array[ loopVariable ] with array[ randomNumber ]



I haven't sat down and tried this, because I'm in a hurry, but I'm pretty sure it will work. Just another possible way of doing it.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Card Shuffle

Posted 21 September 2010 - 05:47 PM

or use can use an ArrayList of cards
put all your cards into it
and randomly retreive cards from it
// Deck of cards
Cards[] card = new Cards[52];
// init it and put it in an ArrayList
ArrayList<Cards> al = new ArrayList<Card>();
for(int i = 0; i < 52; i++) {
   card[i] = new Card(....
   al.add(card[i]);
}

// Random
Randon ran = new Random();
// get a deck of five cards
Cards[] deck = new Card[5];
for(int i = 0; i < deck.length; i++) {
   int index = ran.nextInt(al.size());    .. random pos in the 52, 51, 50... lasting cards
   deck[i] = al.remove(index);
}


Was This Post Helpful? 0
  • +
  • -

#5 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Random Card Shuffle

Posted 21 September 2010 - 09:19 PM

[Deleted by Unknown]

This post has been edited by x68zeppelin80x: 22 September 2010 - 05:11 PM

Was This Post Helpful? -1
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Random Card Shuffle

Posted 21 September 2010 - 09:25 PM

x68zeppelin80x, how can you expect the OP to learn if you just HAND them code? Rule number 1 says "We will not do your homework for you! Do not ask us to give you code!". If you are going to just give code like that, don't just say "Here it is"; make sure you explain it.
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Card Shuffle

Posted 22 September 2010 - 04:39 PM

And having the String[] suits and faces in the Deck class rather than in the Card class is a very poor design as far as I am concerned
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1