#! /usr/bin/perl -w use strict; sub normdist { my (\$u1, \$u2); # uniformly distributed random numbers my \$w; # variance, then a weight my (\$g1, \$g2); # gaussian-distributed numbers do { \$u1 = 2 * rand() - 1; \$u2 = 2 * rand() - 1; \$w = \$u1*\$u1 + \$u2*\$u2; } while ( \$w >= 1 ); \$w = sqrt( (-2 * log(\$w)) / \$w ); \$g2 = \$u1 * \$w; \$g1 = \$u2 * \$w; # return both if wanted, else just one return wantarray ? (\$g1, \$g2) : \$g1; } sub check1 { my (\$diff) = @_; my (\$x1, \$x2) = normdist; my (\$x3, \$x4) = normdist; \$x3 += \$diff; \$x4 += \$diff; my \$left = \$x1 < \$x3 && \$x1 < \$x4 && \$x2 < \$x3 && \$x2 < \$x4; my \$right = \$x1 > \$x3 && \$x1 > \$x4 && \$x2 > \$x3 && \$x2 > \$x4; return \$left || \$right; } sub do_panel { my (\$diff, \$size) = @_; my \$ttl = 0; for (my \$ii = 0; \$ii < \$size; \$ii++){ \$ttl += check1(\$diff); } return \$ttl / \$size; } main: { my \$panelsize = 100; for (my \$jj = 0; \$jj < 225; \$jj++){ my \$diff = \$jj / 50; my \$rslt = do_panel(\$diff, \$panelsize); print STDOUT "\$diff, \$rslt,\n"; } }