user
Matt Parker of the "Stand Up Maths channel" has a   YouTube video   of a card trick that creates a semblance of order from chaos.

The task is to simulate the trick in a way that mimics the steps shown in the video.

1. Cards.

1. Create a common deck of cards of 52 cards   (which are half red, half black).
2. Give the pack a good shuffle.

2. Deal from the shuffled deck, you'll be creating three piles.

1. Assemble the cards face down.
   1. Turn up the   *top card*   and hold it in your hand.
      1. if the card is   black,   then add the   *next*   card (unseen) to the "black" pile.
      2. If the card is     red,    then add the   *next*   card (unseen) to the   "red"  pile.
   2. Add the   *top card*   that you're holding to the **discard** pile.   (You might optionally show these discarded cards to get an idea of the randomness).
2. Repeat the above for the rest of the shuffled deck.

3. Choose a random number   (call it **X**)   that will be used to swap cards from the "red" and "black" piles.

1. Randomly choose   **X**   cards from the   "red"  pile (unseen), let's call this the   "red"  bunch.
2. Randomly choose   **X**   cards from the "black" pile (unseen), let's call this the "black" bunch.
3. Put the     "red"    bunch into the   "black" pile.
4. Put the   "black"   bunch into the     "red"  pile.
5. (The above two steps complete the swap of   **X**   cards of the "red" and "black" piles.   
    (Without knowing what those cards are --- they could be red or black, nobody knows).

4. Order from randomness?

1. Verify (or not) the mathematician's assertion that:

```
     The number of black cards in the "black" pile equals the number of red cards in the "red" pile. 

```

(Optionally, run this simulation a number of times, gathering more evidence of the truthfulness of the assertion.)

Show output on this page.
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2018.08

```
# Generate a shuffled deck
my @deck = shuffle;
put 'Shuffled deck:          ', @deck;

my (@discard, @red, @black);
# Deal cards following task description
deal(@deck, @discard, @red, @black);

put 'Discard pile:           ', @discard;
put '"Red"   pile:           ', @red;
put '"Black" pile:           ', @black;

# swap the same random number of random
# cards between the red and black piles
my $amount = ^(+@red min +@black) .roll;
put 'Number of cards to swap: ', $amount;
swap(@red, @black, $amount);

put 'Red pile after swaps:   ', @red;
put 'Black pile after swaps: ', @black;

say 'Number of Red   cards in the Red   pile: ', +@red.grep('R');
say 'Number of Black cards in the Black pile: ', +@black.grep('B');

sub shuffle { (flat 'R' xx 26, 'B' xx 26).pick: * }

sub deal (@deck, @d, @r, @b) {
    while @deck.elems {
        my $top = @deck.shift;
        if $top eq 'R' {
            @r.push: @deck.shift;
        }
        else {
            @b.push: @deck.shift;
        }
        @d.push: $top;
    }
}

sub swap (@r, @b, $a) {
    my @ri  = ^@r .pick($a);
    my @bi  = ^@b .pick($a);
    my @rs  = @r[@ri];
    my @bs  = @b[@bi];
    @r[@ri] = @bs;
    @b[@bi] = @rs;
}

```

Sample output:

```
Shuffled deck:          B B B R B R R R B B R R R B R B R B R R R B R B B R R B B R R B R B R R R R B R R B B B B B B R R B B B
Discard pile:           B B B R B R R R R R R R B R B R R R R B R B B B R B
"Red"   pile:           R R B B B R B B B B B R R B B
"Black" pile:           B R R B R R R B B R B
Number of cards to swap: 6
Red pile after swaps:   R R B B R R R R B B B R B B B
Black pile after swaps: B R B R R B B B B R B
Number of Red   cards in the Red   pile: 7
Number of Black cards in the Black pile: 7
```