Neither is considering the affect it has on the randomness of the result
Well, actually it is!
Given the $size of the test, the probability of salva2 generating a string starting by two equal characters is 1/($size+1) instead of the 1/$size in the other methods.
The funny thing is that salva2 is unbiased, they are the other methods that are actually biased!!!
The following code counts the number of repetitions at any given position in the generated strings:
@set = (1..3);
$max_reps = 2;
$len = 10;
my @acu = ([], []);
my @gen = (\&gen_salva, \&gen_salva2);
my ($total, $ds) = (0,0);
my $n = 10000000;
for (1..$n) {
for (0, 1) {
my $out = $gen[$_]->();
while ($out =~ /(.)\1/g) {
$acu[$_]->[pos($out) - 2]++;
}
}
}
use Data::Dumper;
print Dumper \@acu;
And this is the result:
salva => [ 3333044, 2222094, 2591285, 2469706, 2509595, 2496496, 2501
+039, 2501529, 2497584 ]
salva2 => [ 2500639, 2498220, 2499044, 2501878, 2500880, 2500962, 2498
+685, 2499675, 2508077 ]
So, with salva2 the probability of finding a repetition at any position is 1/($size+1) while with the rest of methods the probability varies between the maximum 1/$size (at position 0) and the minimum ($size-1)/$size**2 (at position 1). |