Randomize deck of cards using constraints

In reply to bharat_vg:

It would be far easier to generate a deck of 52 cards in order, then use the built-in cards.shuffle() method.

Much harder would be to make sure each card is unique using a nested foreach loop

constraint C_unique { foreach (cards[ii]) foreach (cards[jj]) 
    ii != jj -> cards[ii].suit != cards[jj].suit && cards[ii].rank != cards[jj].rank; }

I do not think you need to be concerned with the color as that will naturally fall out with the suit constraint.

1 Like