Shop Learn P1 Docs P2 Docs
Convincing Myself of a Puzzle's Answer (Random Numbers with the P2) — Parallax Forums

Convincing Myself of a Puzzle's Answer (Random Numbers with the P2)

A recent Veritasium video got me wondering. As the video title suggests, the answer to the puzzle doesn't seem right.

Here's the video:

I stopped the video six minutes in so I could convince myself the puzzle's answer really was correct.

Writing a program to generate random prisoner combinations was fun. I started out with just using ten prisoners and ten boxes until I felt confident I have the sorting logic correct.

Even though I know the success rate should be around 30%, I was still really surprised to see this was correct.

There has got to be better algorithms than the way I did this. My system of filling the boxes with random numbers seems awkward but I felt like it would be cheating to search the internet for a better algorithm.

Here's a section of the output from one run of a hundred sets.

0:sortIndex = 87
0:Debug All Boxes, sortIndex = 87
0:box[0] = 54, 62, 26, 3, 48, 23, 16, 22, 39, 6, 68, 52, 80, 25, 20, 2, 0, 74, 91, 11
0:box[20] = 82, 72, 30, 8, 61, 60, 96, 7, 71, 35, 83, 45, 18, 24, 93, 78, 12, 65, 63, 90
0:box[40] = 41, 79, 85, 81, 21, 9, 51, 92, 64, 57, 50, 73, 17, 40, 15, 98, 10, 47, 37, 70
0:box[60] = 19, 32, 33, 95, 76, 94, 84, 5, 13, 27, 59, 28, 38, 97, 87, 58, 99, 46, 56, 1
0:box[80] = 89, 43, 86, 53, 4, 31, 49, 77, 36, 29, 88, 42, 75, 69, 67, 55, 44, 14, 34, 66
0:End FindLoops(), successes = 100
0:completeSuccessCount = 28. All convicts are freed.

Index 88 through 98 were cut.

0:sortIndex = 99
0:Debug All Boxes, sortIndex = 99
0:box[0] = 0, 53, 44, 35, 50, 18, 89, 34, 61, 52, 84, 96, 25, 16, 56, 95, 33, 30, 3, 20
0:box[20] = 91, 85, 67, 82, 65, 8, 6, 24, 71, 55, 31, 70, 36, 43, 38, 72, 73, 83, 17, 7
0:box[40] = 88, 77, 62, 4, 64, 75, 2, 92, 14, 29, 13, 90, 76, 26, 94, 32, 58, 5, 15, 23
0:box[60] = 21, 59, 39, 28, 22, 27, 74, 86, 10, 60, 51, 93, 42, 63, 87, 68, 37, 80, 40, 49
0:box[80] = 19, 46, 78, 98, 45, 1, 81, 99, 57, 66, 11, 97, 47, 54, 79, 12, 9, 69, 41, 48
0:End FindLoops(), successes = 38
0: Only 38 out of 100 convicts tested were successful. All are executed.
CON '' 0:Final completeSuccessCount = 28, numberOfSorts = 100
0: End program.

The attached program uses my funky multi port serial object. The object includes a lock so it can be used from multiple cogs. Sorry for not cleaning things up more but I didn't really want to spend much more time on this. I'll be happy to answer any questions about my strange serial object.

You should use Parallax Serial Terminal.exe for the output. I don't think the normal debug window will display the data correctly.

If any of you care to look at my code, I'd be very interested to hear how the algorithms could be improved.

Comments

  • Hello Duane,
    I checked out the video and looked over your code briefly, so did you use an algorithm that I have seen to shuffle a deck of cards contained in an array?

Sign In or Register to comment.