4

I want to implement a poker game in Java. So I made a card class and created 52 card objects and put them into a array. This array is the card deck.

How would I randomly shuffle this array of objects? Is there an inbuilt method or any other way?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Daybreaker
  • 1,037
  • 5
  • 20
  • 42

3 Answers3

6

Use ArrayList instead and use Collections.shuffle()

Collections.shuffle(yourListInstance);
jmj
  • 237,923
  • 42
  • 401
  • 438
1

I've modified the top answer from "Random shuffling of an array" to work for an array of Card objects. I simply changed int[] arCard[] ar, and int a = ar[index]Card a = ar[index]

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

// Implementing Fisher–Yates shuffle
static void shuffleArray(Card[] ar)
{
  // If running on Java 6 or older, use `new Random()` on RHS here
  Random rnd = ThreadLocalRandom.current();
  for (int i = ar.length - 1; i > 0; i--)
  {
    int index = rnd.nextInt(i + 1);
    // Simple swap
    Card a = ar[index];
    ar[index] = ar[i];
    ar[i] = a;
  }
}
Community
  • 1
  • 1
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
0

Collections.shuffle isn't good enough:

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 randomness 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.

You need a source of randomness producing a number that is exactly distributed uniformly between 1 and 52! (inclusive).

Lorenzo Gatti
  • 1,260
  • 1
  • 10
  • 15
  • I'm sorry, I don't quite follow... What is wrong with `Collections.shuffle()` exactly? It seems to me that the description just says that a PRNG is used instead of a true RNG, which makes sense. Also, I'm not quite following your last requirement. Why do you need a source of randomness that produces exactly that range? – awksp Jun 25 '14 at 14:56
  • You can provide your own random generator to `shuffle()`. – assylias Jun 25 '14 at 15:10
  • I didn't get what u hv said. Bt it seems their is a biased situation in `Collections.shuffle()` – Daybreaker Jun 25 '14 at 15:11
  • @Daybreaker That's more a problem with PRNGs in general than `Collections.shuffle()` itself. The shuffling method implemented in `Collections.shuffle()` can be perfectly random if you can provide a true RNG. – awksp Jun 25 '14 at 15:19
  • If the "source of randomness" (actually the PRNG) doesn't produce each possible permutation with equal probability the result of Collections.shuffle() is biased. The quoted Javadoc excerpt appears an admission that Collections.shuffle() takes shortcuts, such as blindly applying the modulo operator to PRNG numbers rather than rejecting them as necessary, on top of not seeding the PRNG randomly enough. – Lorenzo Gatti Mar 11 '15 at 17:12