#!/usr/bin/perl -w sub objective { return sqrt(2); } my $N = 100; # size of each run my $candidates = 10; # size of the best's list my $min = 0; my $max = +100; my (@matrix, @list); print "Value Error\n"; while (1) { for my $i (0..$N-1) { $matrix[$i][0] = $min + rand()*($max-$min); # value $matrix[$i][1] = 2 - $matrix[$i][0]*$matrix[$i][0] ; # signed error } @list = sort { abs($a->[1]) <=> abs($b->[1]) } @matrix; ; $min = $list[$candidates-1][0]; # value of the worst candidate for my $i (reverse 0..$candidates-2){ if ($list[$candidates-1][1] * $list[$i][1] < 0) { # value of a better candidate $max = $list[$i][0]; # by the other side last; } } if ($max < $min ) { ($min , $max ) = ( $max, $min); } print "$list[0][0] $list[0][1]\n"; if ($list[0][1] == 0 || "$list[0][0]" eq "$list[$candidates-1][0]") { print objective(), "\n"; exit 0; } } __END__ Monte Carlo simulation