in reply to Difficulty in randomization

use 5.010; use strict; # A few definitions... use constant { ITERATIONS => 20, # number of random strings to generate LENGTH => 72, # length of each string X_COUNT => 4, # number of X characters in each string }; # We need a uniq() function. List::MoreUtils provides a fast XS based # one, but if they don't have it installed, fall back to a pure Perl # alternative. BEGIN { *uniq = eval { require List::MoreUtils; 1 } ? \&List::MoreUtils::uniq : sub (@) { my %seen = (); grep { not $seen{$_}++ } @_ }; } # We want to generate more than one random string. foreach my $iter (1 .. ITERATIONS) { # Make a pool of random numbers. uniq ensures no duplicates, # but means that we might end up with fewer than we need, # hence a loop to top up the pool if it's below the number # we need. my @positions; while (@positions < X_COUNT) { push @positions, map { int rand LENGTH } 0 .. X_COUNT; @positions = uniq @positions; } # Take just the first X_COUNT items from our pool, then # map it into a hash structure. my %exes = map { $_ => 1 } @positions[0 .. X_COUNT-1]; # Create our array of length LENGTH. Indexes found in the # %exes hash get an "X"; other indexes get a hyphen. my @array = map { $exes{$_} ? 'X' : '-' } 1 .. LENGTH; # Print out the array as a string. say join q{}, @array; }