Random values ​​and duplicate Java

I have an array ( cards) of 52 maps (13x4) and another array ( cardsOut) of 25 maps (5x5). I want to copy elements from 52 cards to a 25-card array randomly.

Also, I don't want duplicates in a 5x5 array. So here is what I have:

        double row=Math.random() *13;
        double column=Math.random() *4;

        boolean[][] duplicates=new boolean[13][4];

        pokerGame[][] cardsOut = new pokerGame[5][5];
        for (int i=0;i<5;i++)
            for (int j=0;j<5;j++){
                if(duplicates[(int)row][(int)column]==false){
                    cardsOut[i][j]=cards[(int)row][(int)column];
                    duplicates[(int)row][(int)column]=true;
                }
            }

2 problems in this code. First, random values ​​for a row and column are generated only once, so the same value is copied to the 5x5 array each time. Since the same values ​​are copied every time, I'm not sure if my double check is very efficient, or if it works at all.

How to fix it?

+5
source share
2 answers
+7

, , . card - CardRemaining CardsUsed.

CardRemaining 52 , CardsUsed . CardUsed, CardRemaining, .

( #):

  class Program
    {
        static void Main(string[] args)
        {
            List<Card> CardsRemaining=new List<Card>();
            for (int i = 0; i < 12; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Card c = new Card(i, j);
                    CardsRemaining.Add(c);
                }
            }
            List<Card> CardsUsed = new List<Card>();
            for(int i=0;i<25;i++)
            {
                int cardIndex = getRandomNumber(CardsRemaining.Count);
                Card c = CardsRemaining[cardIndex];
                CardsRemaining.Remove(c);
                CardsUsed.Add(c);
            }
        }
    }

    class Card
    {
        public int Number;
        public int Color;
        public Card(int number, int color)
        {
            this.Number = number;
            this.Color = color;
        }
    }
+5

All Articles