#!/usr/bin/perl
push(@INC,"/www/cgi-bin");
require "cgi-lib.pl";
&ReadParse;
print &PrintHeader;

#
# 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?
#

# Get Variables from web browser

$r = $in{r};	# Total number of coins
$s = $in{s};	# Number of special coins
$h = $in{h};	# Number of needed heads
$t = 10000;	# Number of trials

# 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 <<"EndOfHTML"
<html>
<head>
<title>Results</title>
</head>
<body>
<p align=center>
Number of coins total: $r
<br>
Number of special coins: $s
<br>
Number of heads needed: $h
<p align=center>
Result: $z
</body>
</html>
EndOfHTML
