jeudi 2 juillet 2020

Randomly select objects once from deck of cards

I pretty sure my remainder operator is in the wrong place, I've tried a few different variations, program still runs but pulls the same card more than once:

Card [] deck = {card0, card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13,
        card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26, card27, card28,
            card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39, card40, card41, card42, card43,
            card44, card45, card46, card47, card48, card49, card50, card51,
        };
for (int i = 0; i<deck.length;i++){
    deck[i] = deck[i%51];
    }
for (int i = 0; i<deck.length; i++){
    index = (int)(Math.random()*deck.length);
    deck[index] = deck[i];

    System.out.println(deck[i] + " ");

    }

I know the deck isn't created in the most efficient manner, that's due to limitations of the assignment. I've only included a small piece of the code, because I think the issue is with my for loop and remainder operator.




Aucun commentaire:

Enregistrer un commentaire