#!/usr/bin/perl use strict; use warnings; my $n = 1e4; # number of runs for the statistics my $N = 32; my @sum; my @max = (0)x($N+1); for (1..$n) { my @have_num = (); for my $i (1..$N) { my $attempts = 0; while(1) { my $rand = int(rand($N)); #print "."; $attempts++; unless ($have_num[$rand]) { #print " $i: $rand\n"; $have_num[$rand] = 1; last; } } $sum[$i] += $attempts; $max[$i] = $attempts if $attempts > $max[$i]; } } for my $i (1..$N) { my $avg = $sum[$i]/$n; my $bar = "*" x int($avg+0.5); printf "%2d: %6.3f [%6.3f] %s max=%d\n", $i, $avg, $N/($N-$i+1), $bar, $max[$i]; } __END__ 1: 1.000 [ 1.000] * max=1 2: 1.033 [ 1.032] * max=4 3: 1.065 [ 1.067] * max=4 4: 1.104 [ 1.103] * max=5 5: 1.142 [ 1.143] * max=7 6: 1.186 [ 1.185] * max=7 7: 1.230 [ 1.231] * max=8 8: 1.283 [ 1.280] * max=8 9: 1.334 [ 1.333] * max=8 10: 1.392 [ 1.391] * max=10 11: 1.453 [ 1.455] * max=10 12: 1.521 [ 1.524] ** max=11 13: 1.601 [ 1.600] ** max=13 14: 1.684 [ 1.684] ** max=13 15: 1.779 [ 1.778] ** max=14 16: 1.882 [ 1.882] ** max=16 17: 1.999 [ 2.000] ** max=19 18: 2.137 [ 2.133] ** max=17 19: 2.283 [ 2.286] ** max=23 20: 2.462 [ 2.462] ** max=20 21: 2.674 [ 2.667] *** max=26 22: 2.904 [ 2.909] *** max=29 23: 3.198 [ 3.200] *** max=31 24: 3.551 [ 3.556] **** max=37 25: 3.996 [ 4.000] **** max=48 26: 4.554 [ 4.571] ***** max=47 27: 5.331 [ 5.333] ***** max=55 28: 6.400 [ 6.400] ****** max=79 29: 8.026 [ 8.000] ******** max=95 30: 10.597 [10.667] *********** max=140 31: 15.983 [16.000] **************** max=211 32: 32.092 [32.000] ******************************** max=343