$n=10; $common=0; $top[0]=0; $top[$n]=1; $min_p=1; for(1..$n-1) { $top[$_]=rand; } @top = sort @top; for(1..$n) { $p[$_]=$top[$_]-$top[$_-1]; if ($p[$_]>$max_p) { $max_p=$p[$_]; $best=$_; } if ($p[$_]<$min_p) { $min_p=$p[$_]; $worst=$_; } } while(1) { if ($common==0) { $adam=1; $oldest=1; $last=1; $ritardista=1; $another=1; } else { $adam=$common; $ritardista=$oldest; $another=$last; } $dio=$best; $idiot=$worst; $x=rand; $i=1; while($x>$top[$i]) { $i++; } $trials++; if ($i==$adam) { $adam_s++; } if ($i==$ritardista) { $rita_s++; } if ($i==$dio) { $dio_s++; } if ($i==$another) { $anot_s++; } if ($i==$idiot) { $idio_s++; } if ($trials%100==0) { print $dio_s/$trials." ".$adam_s/$trials." ".$anot_s/$trials." ".$rita_s/$trials." ".$idio_s/$trials."\n"; } $last=$i; $count[$i]++; if ($count[$i]>$count[$common]) { $common=$i; print "Adam changes at trial ".$trials."\n"; } $max_age=0; for(1..$n) { if($_==$i) { $age[$_]=0; } else { $age[$_]++; if($age[$_]>$max_age) { $max_age=$age[$_]; $oldest=$_; } } } }