#! /usr/bin/perl -w use strict; use Getopt::Std; use vars qw(@nums $opt_n $opt_N); getopts("n:N:"); use Data::Dumper; $Data::Dumper::Indent = 1; if ($opt_n) { @nums = split /:/, $opt_n; unless (2 == @nums) { die "Need -n option to have an argument [number:number]\n"; } } else { @nums = (ask_num(), ask_num()); } unless ($opt_N) { print "How many times should I run this: "; $opt_N = ; chomp($opt_N); } for (1..$opt_N) { my %info = run_experiment(@nums); print Dumper(\%info); ; } sub ask_num { print "Please enter a number: "; my $num = ; chomp($num); return $num; } sub flip_coin { 0.5 > rand() ? 1 : 0; } sub guess_other_num { return (log(rand) * (flip_coin() ? 10 : -10 )); } sub run_experiment { my %status; @status{'low', 'high'} = sort {$a <=> $b} @_; if (flip_coin()) { $status{"gave_high"} = 1; @status{"guess_high", "aux"} = think_high($status{high}); } else { $status{"gave_high"} = 0; @status{"guess_high", "aux"} = think_high($status{low}); } $status{is_right} = ($status{guess_high} == $status{gave_high}); if ($status{low} < $status{aux} and $status{aux} < $status{high}) { $status{is_guaran} = 1; } else { $status{is_guaran} = 0; } if (wantarray) { return %status; } else { return $status{is_right}; } } sub think_high { my $num = shift; my $other_num = guess_other_num(); my $guess = $num > $other_num; return wantarray() ? ($guess, $other_num) : $guess; }