Talk:Mind boggling card trick
Original blogpost
I had originally written a blog post on the video and thought now would be a good time to see if it makes an RC task. --Paddy3118 (talk) 12:48, 25 August 2018 (UTC)
- I think it's a humdinger of a Rosetta Code task; I spent some time in thinking of which way to go: treat the card deck as a list, or treat it as an array. In the end, I thought that (for REXX), a list would be more idiomatic and easier to code. -- Gerard Schildberger (talk) 06:52, 26 August 2018 (UTC)
thoroughness of shuffling the deck
I found (using the REXX program) that when doing any number of shuffles (including zero), the assertion holds.
I did some digging into my (REXX) code, and found that the creation of a "new" card deck (as it would come out of the manufacturer's box), was flawed.
An almost perfect shuffle was being created by generating a sequence of numbers such that it appeared that there was a red card, a black card, a red card, a black card ...
I rewrote the create subroutine to mimic a new playing card deck 13 spades in order, 13 hearts, 13 clubs, 13 diamonds.
This was a bit of overkill, but now one could divide by 4 and use the remainder (same as modulus in this case) and find which suit the card was:
(0 = spaces, 1 = hearts, 2=clubs, and 3 = diamonds).
The pip could be found out by dividing by 13 and adding 1, but the pips weren't important for this card trick.
After finding out that it didn't make a difference, I left the new method of creating a new playing deck in, as it didn't hurt, and I would probably need a creation routine to generate a new playing card deck in the future.
Or, of course, you could just divide by 2 to test if it was a red or black card red = 1 (or odd), black = 0 (or even).
Then, I had the REXX program do various number of shuffles, including zero. It didn't matter.
All that was necessary was a deck of playing cards that were (exactly) half red, half black,
no matter what the order of the playing cards were and no matter how many shuffles (if any) were performed.
The REXX program allows an odd-numbered amount of playing cards, and then the assertion starts failing (with interesting, if not amusing, results).
Even if the shuffled (or not shuffled) card deck 1st half was all read, and the 2nd half was all black.
By the way, this could produce a situation where one of the "red" or "black" piles had no cards in it, and cause my random swap cards routine to fail when generating a random number from 1 to the number of cards, and the REXX random BIF routine coughed up an error code. I fixed that by testing for a "null" (empty) pile of cards. This won't happen if the playing card deck was already randomized by generating a neat sequence of card (odd/even, odd/even ... repeated, or red/black, red/black, ... repeated).
A single shuffle (as used in the REXX program) exchanged two random cards, two shuffles exchanged four random cards, the shuffle was performed two cards at a time, so it is possible to shuffle a card that was previously shuffled, and in somewhat rare occasion, exchange the same card (that is, no effective shuffle). So, for a very few shuffles, instead of 26 shuffles, maybe 25 were effectively performed. Meh. It wasn't worth the (CPU) time to check for that special case, as the REXX program was performing a hundred thousand simulations at a time, and each simulation performed (at that time) 26 shuffles. (for the initial program test). I wonder how many electrons I killed for this Rosetta Code task. But no trees were felled, by gum! An atom walked up to another atom and said, "Did you know scientists found out that electrons have mass? "No", said the other, I didn't know they were Catholic!" -- Gerard Schildberger (talk) 01:07, 29 August 2018 (UTC)