#!/usr/bin/perl

# 2/3 of the time it will stay on the outside and 1/3 of the time it will go into the middle.
# When in the middle, it has a 100% chance of going back to the outside.

$x = 0;		# Inside counter
$y = 0;		# Outside counter
$t = 100000; 	# Trials

for ($i = 0; $i < $t; $i++) {
  if(rand(3) < 1) {
    $x++;
    $y++;
    $i++;
  }
  else {
    $y++;
  }
}

$xp = ($x / $t) * 100;
$yp = ($y / $t) * 25;	# Only divided by 25 because it is multiplied by 100 and divided by 4
			# To represent the percentage for (100) and the four outside boxes (4)
print "The rat spends $xp% in the middle box and $yp% in each of the four outside boxes.\n";

