#! perl -slw use strict; use Data::Dump qw[ pp ]; use Algorithm::Combinatorics qw[ variations_with_repetition ]; our $N //= 4; my @perms = map join( '', @$_ ), variations_with_repetition( [ 1 .. $N ], $N ); my %lookup; $lookup{ $perms[ $_ ] } = $_ for 0 .. $#perms; my @counts = (0) x @perms; for( 1 .. 1e3 ) { my $observed = join '', map 1+int( rand $N ), 1 .. $N; ## random observation ++$counts[ $lookup{ $observed } ]; } printf "%${N}s : %${N}d\n", $perms[ $_ ], $counts[ $_ ] for 0 .. $#perms;