There is still randomness present. The discard stack shown above seems pretty random.

Swap the same, random, number of cards between the two stacks.

We can't swap more than the number of cards in a stack.

In [64]:

max_swaps=min(len(black_stack),len(red_stack))

Randomly choose the number of cards to swap. (You could use a dice).

In [65]:

swap_count=random.randint(0,max_swaps)print('Swapping',swap_count)

Swapping 11

Randomly choose that number of cards out of each stack to swap. (Without knowing those cards - they could be red or black cards from the stacks, we don't know)

In [66]:

defrandom_partition(stack,count):"Partition the stack into 'count' randomly selected members and the rest"sample=random.sample(stack,count)rest=stack[::]forcardinsample:rest.remove(card)returnrest,sampleblack_stack,black_swap=random_partition(black_stack,swap_count)red_stack,red_swap=random_partition(red_stack,swap_count)

Perform the swap.

In [67]:

black_stack+=red_swapred_stack+=black_swap

Order from randomness?

The mathematician asserts that:

The number of black cards in the black pile equals the number of red cards in the red pile