vendredi 28 avril 2017

Randomness of shuffled cards

I have an Array List of cards containing 52 cards. I want to shuffle the deck.

This is what I have done.

  1. Created a new Array List.
  2. Generated random index between 0 to deck size.
  3. get card at random index and add to new list.
  4. remove card from deck
  5. repeat untill deck is empty.

Here is my code:

String[] Number = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] Suits = {"Club","Diamonds","Hearts","Spades"};
ArrayList<Card> deck = new ArrayList<Card>();

// create a deck    

for(int i=0;i<13;i++){
        for(int j=0;j<4;j++){
            Card card = new Card(Suits[j],Number[i]);
            deck.add(card);
        }    
    }

// shuffle of deck

ArrayList<Card> new_deck = new ArrayList<Card>();
    while(deck.size()!=0){   

        Random rand = new Random();
        int  n = rand.nextInt(deck.size());

        new_deck.add(deck.get(n));
        deck.remove(n);
    }

// Display

for(int i=0;i<52;i++){
        System.out.println(new_deck.get(i).getSuit()+" : "+new_deck.get(i).getValue());
    }

Finally, I get shuffled deck from new ArrayList.

Does it's randomness is enough or not ?

What should I do to increase randomness ?

Any kind of suggestion and help is appreciated.




Aucun commentaire:

Enregistrer un commentaire