#!/usr/bin/perl $boxes = shift() || 3; $runs = shift() || 1000; $pick_random = shift() || 0; $noswitch = 0; $switch = 0; print "Running simulated box problem for: $boxes boxes for $runs runs\n"; print "Pick: " . ($pick_random ? 'Random' : 'Always Same') . "\n"; print "-" x 79, "\n"; printf( '%5s %10s %10s %20s %20s', 'Run', 'Stay/Win', 'Switch/Win', 'Total Stay/Win', 'Total Switch/Win' ); print "\n"; print "-" x 79, "\n"; for( 1 .. $runs ) { $rand = int( rand( $boxes ) ); $choice = $pick_random ? 0 : int( rand( $boxes ) ); if( $rand == $choice ) { $noswitch++; $noswitch_win = 'YES'; $switch_win = 'NO'; }else{ $switch++; $noswitch_win = 'NO'; $switch_win = 'YES'; } printf( '%5d %10s %10s %20d %20d', $_, $noswitch_win, $switch_win, $noswitch, $switch ); print "\n"; } print "-" x 79, "\n"; printf( 'Final Probability: %3.2f%% win to Stay, %3.2f%% win to Switch', $noswitch / $runs * 100, $switch / $runs * 100 ); print "\n";