#!/usr/bin/perl -w

# Who:   Javed A. Aslam
# What:  restaurantMC.pl
# When:  10/24/08
# Why:   Generates a sequence according to restaurant Markov chain
# How:   markov.pl <length>

if (@ARGV != 1) {
  die "\nUsage: restaurantMC.pl <length> \n\n";
  }

$n = shift;

# Round 1...

$state = "B";
print "\nB";
$countB = 1;
$countM = 0;
$countS = 0;
$n--;

# Subsequent rounds...

while ($n--) {
  $frac = rand();

  if ($state eq "B") {
    if ($frac > .8) {
      $state = "M";
      }
    elsif ($frac > .7) {
      $state = "S";
      }
    }
  elsif ($state eq "M") {
    if ($frac > .7) {
      $state = "B";
      }
    elsif ($frac > .6) {
      $state = "S";
      }
    }
  else {
    if ($frac > .7) {
      $state = "B";
      }
    elsif ($frac > .5) {
      $state = "M";
      }
    }

  print "$state";

  if ($state eq "B") {
    $countB++;
    }
  elsif ($state eq "M") {
    $countM++;
    }
  else {
    $countS++;
    }

  }

print "\n\nCounts: B=$countB; M=$countM; S=$countS\n";

$total = $countB + $countM + $countS;
$countB /= $total;
$countM /= $total;
$countS /= $total;

print "\nProb: B=$countB; M=$countM; S=$countS\n\n";

