Java random values and duplicates

Posted by f-Prime on Stack Overflow See other posts from Stack Overflow or by f-Prime
Published on 2010-12-22T01:50:26Z Indexed on 2010/12/22 1:54 UTC
Read the original article Hit count: 520

Filed under:
|
|

I have an array (cards) of 52 cards (13x4), and another array (cardsOut) of 25 cards (5x5). I want to copy elements from the 52 cards into the 25 card array by random.

Also, I dont want any duplicates in the 5x5 array. So here's 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, the random values for row and column are only generated once, so the same value is copied into the 5x5 array every time. Since the same values are being copied every time, I'm not sure if my duplicate checker is very effective, or if it works at all.

How do I fix this?

© Stack Overflow or respective owner

Related posts about java

Related posts about random