#!/afs/athena/contrib/perl5/perl


MAIN: for ($trial=0;$trial<100;$trial++){
    @cand = ();

    $cct = $curavg = 0;
    $picky = 1.2;

    $total = int(rand(rand(60))+1);
    $max = int(rand(100000));
    for $i (1..$total){
	push(@cand, rand($max));
    }

    $c = shift(@cand);
    while(&strategy($c) ne "keep") {
	if($#cand == -1){
	    print "Strategy loses $max/$total\n";
	    next MAIN;
	}
	else{
	    $c = shift(@cand);
	}
    }
    print "Kept $c.  Max possible was $max of $total candidates\n";
    $totperf +=($c/$max);
}

$avgperf = $totperf/100;
print "Average 'score' as percentage of whats possible: $avgperf\n";

sub strategy {
    $cnd = shift;

    $newavg = ($curavg*$cct) +$cnd;
    $cct++;
    $newavg /= $cct;
    $curavg = $newavg;
    $goal = $picky*$curavg;
    if($cnd > $goal){
	print "Accepting candidate # $cct\n";
	return "keep";
    }
}
