The 100 prisoners riddle

pwe

Bronze Baronet of the Realm
881
6,137
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;
        }
    }
}
 
  • 1Like
  • 1Rustled
Reactions: 1 users

moonarchia

The Scientific Shitlord
21,253
38,605
There is a 100% chance that all 100 prisoners will be raped by Foler and Vanessa.
 
  • 1Galaxy Brain
  • 1Like
Reactions: 1 users

Gravel

Mr. Poopybutthole
36,190
114,652
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
18,449
21,188
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: 1 user

Tuco

I got Tuco'd!
<Gold Donor>
45,393
73,466
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: 1 users

Gravel

Mr. Poopybutthole
36,190
114,652
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: 1 user

Izo

Tranny Chaser
18,449
21,188
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?
 
  • 1Like
Reactions: 1 user

Hoss

Make America's Team Great Again
<Gold Donor>
25,510
11,972
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
12,326
11,706
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.
 

Ambiturner

Ssraeszha Raider
16,040
19,499
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.

Just now seeing this thread, and agree it actually seemed obvious as soon as he started explaining it.

Your example of adding 1 to each doesn't follow the rule of it being random, though, which the problem relies on
 

Hoss

Make America's Team Great Again
<Gold Donor>
25,510
11,972
Your example of adding 1 to each doesn't follow the rule of it being random, though, which the problem relies on

It's been a while since I watched the video, but I think that was the point. They mentioned the sadistic warden making it a loop of 100 and it blew my mind that you could defeat that by adding 1 to all the numbers.
 

Ambiturner

Ssraeszha Raider
16,040
19,499
It's been a while since I watched the video, but I think that was the point. They mentioned the sadistic warden making it a loop of 100 and it blew my mind that you could defeat that by adding 1 to all the numbers.

Didn't watch the whole video, so not sure if they mentioned this but you'd hope that after 49 straight boxes that were +1 from the previous box, you'd be able to take an educated guess and go 1 under your original number
 

Tuco

I got Tuco'd!
<Gold Donor>
45,393
73,466
Another 100 prisoners, this time with green eyes.


This makes sense but it's gone through my mind a few times after watching it.
 
  • 1Like
Reactions: 1 user

Furry

WoW Office
<Gold Donor>
19,450
24,499
Didn't watch the whole video, so not sure if they mentioned this but you'd hope that after 49 straight boxes that were +1 from the previous box, you'd be able to take an educated guess and go 1 under your original number
Any action that eliminates randomness at an individual level will cause this to become a 50/50. So assuming everyone had a different starting box 1-100, picking a box one above or below your starting box would be exactly the same, as long as 1 is smart enough to realize he should pick 100 if there’s no 0 box.
 

Ambiturner

Ssraeszha Raider
16,040
19,499
Any action that eliminates randomness at an individual level will cause this to become a 50/50. So assuming everyone had a different starting box 1-100, picking a box one above or below your starting box would be exactly the same, as long as 1 is smart enough to realize he should pick 100 if there’s no 0 box.

Think he's saying every box is in the exact same order just +1 added to them all, which would make it a full 100 box loop. That only works if someone is stupid enough to actually keep going with the loop strategy long after the pattern is obvious