#!/usr/bin/perl

#
# coinflip.pl
# Four coins in a bag, one is a double-headed coin.
# If I pick one at random, and flip four heads in a row,
# what is the probablility that I had chosen the trick coin?
#

# Ask user for variables
print "Coinflip Simulation\n";
print "Number of total coins?    "; $r = <STDIN>; chomp($r);
print "Number of trick coins?    "; $s = <STDIN>; chomp($s);
print "Number of required heads? "; $h = <STDIN>; chomp($h);
print "Number of trials?         "; $t = <STDIN>; chomp($t);

# Variables

$c1 = 0;	# Counting variable : trick coins
$c2 = 0;	# Counting variable : regular coins

# Program

for($i = 0; $i < $t; $i++) {
  if(rand($r) < $s) {
    $c1++;
  }
  else {
    if(rand(2**$h) < 1) {
      $c2++;
    }
  }
}

$z = $c1 / ($c1 + $c2);
print "Result: $z\n";
