#!/usr/bin/perl use v5.16; use warnings; use List::Util 'shuffle'; use constant N => 100000; sub doit { my @box = shuffle 0 .. 99; prisoner: for my $prisoner (0 .. 99) { my $box = $prisoner; for (0 .. 49) { my $found = $box[$box]; next prisoner if $prisoner == $found; $box = $found; } return; } 1; } my $success; for (1..N) { $success++ if doit(); } say $success / N; __DATA__ 0.31257