The 100 prisoners riddle

pwe

Ahn'Qiraj Raider
546
4,438
21d 2h 38m
This is pretty cool... I tried to answer it myself, but I was absolutely stumped.


I implemented it for fun, and it does end up exactly as described in the video.

Code can be run online here. Just select .NET 6 as compiler on the left and then paste it in (has to be in that order).

Code:
using System;
using System.Collections.Generic;
using System.Linq;

namespace OnlineRestaurant
{
    class Program
    {
        static void Main(string[] args)
        {
            const int Count = 100;

            int dead = 0;
            const int TestCount = 10000;
            for (int i = 0; i < TestCount; ++i)
            {
                var boxes = GetRandomBoxes(Count);

                for (int n = 0; n < Count; ++n)
                {
                    if (!Find(n, boxes, Count / 2))
                    {
                        dead++;
                        break;
                    }
                }
            }

            double pct = 1.0 - (dead / (double)TestCount);
            Console.WriteLine($"Alive: {TestCount - dead}, dead: {dead}, success rate: {pct:0.00}");
        }

        static List<int> GetRandomBoxes(int count)
        {
            var rnd = new Random();

            var numbers = Enumerable.Range(0, count).ToList();
            var boxes = new List<int>();

            while (numbers.Count > 0)
            {
                int idx = rnd.Next(0, numbers.Count);
                boxes.Add(numbers[idx]);
                numbers.RemoveAt(idx);
            }
            return boxes;
        }

        static bool Find(int n, List<int> boxes, int maxAttempts)
        {
            int attempt = 0;
            int pointer = n;
            while (attempt < maxAttempts)
            {
                if (boxes[pointer] == n)
                    return true;

                pointer = boxes[pointer];
                attempt++;
            }
            return false;
        }
    }
}
 

Gravel

Mr. Poopybutthole
31,167
91,129
239d 23h 2m
Watched the video and found it neat, but it hurt my brain.

Decided I'd show my wife and watch again. She has an applied math degree, and she understood immediately and then walked me through what I didn't understand.

It's a fun fluke and pretty simple concept when you understand. Even the math isn't incredibly advanced that goes into explaining the probabilities.
 

Izo

Tranny Chaser
17,043
15,381
111d 19h 20m
Watched the video and found it neat, but it hurt my brain.

Decided I'd show my wife and watch again. She has an applied math degree, and she understood immediately and then walked me through what I didn't understand.

It's a fun fluke and pretty simple concept when you understand. Even the math isn't incredibly advanced that goes into explaining the probabilities.
1657617650410.png

Your wife is asian?
 
  • 1Worf
Reactions: kroenen

Tuco

I got Tuco'd!
<Gold Donor>
43,107
69,665
105d 17h 6m
That's cool. Random numbers forming loops that can be controlled is a neat property. Good luck getting 99 convicts to follow directions from a nerd.

Similar problem of numerical confusion:

 
  • 2Worf
Reactions: Abefroman and Izo

Gravel

Mr. Poopybutthole
31,167
91,129
239d 23h 2m
Nope, she's white as hell.

She kind of suffered from indecision in college. Think she started with biology and then at some point changed to computer science. But she didn't really love that either. She went to her advisor and asked what the fastest way to graduate would be. Since she had so much math from the computer science path, she ended up just finishing up an applied math degree instead.
 
  • 1Like
Reactions: Izo

Izo

Tranny Chaser
17,043
15,381
111d 19h 20m
That's cool. Random numbers forming loops that can be controlled is a neat property. Good luck getting 99 convicts to follow directions from a nerd.

Similar problem of numerical confusion:

Araysar Araysar what does putin think of this?
 

Hoss

Make America's Team Great Again
<Gold Donor>
23,521
9,499
111d 18h 30m
This was a lot easier to understand than the monty hall problem. I think it's because I had such strong opinions on the monty hall riddle before seeing the experiments. For this one I was stumped so my mind was open. The part blowing my mind was that if the warden was sadistic and purposely made a loop of 100, you can defeat that by adding 1 to all numbers. Seems like you'd all still be fucked.
 

Malakriss

Golden Baronet of the Realm
11,634
10,814
We should teach kids problems like these so they understand the thought process behind turning 100 separate calculations into a single collective one. How would you convert massive individual checks into a single process so it's just examining a random layout rather than adding 100 random choices on top of the random layout.

The base strategy is easy to follow: Pick your own number and follow the number chain, but adding in math on top of that would result in errors and jumping out of loops for the dumber part of the population.