in reply to Re: quick and dirty cryptogram puzzle
in thread quick and dirty cryptogram puzzle

Thanks for the criticism! I've not played many cryptograms, so I wasn't familiar with that idea. I wouldn't have called it a "complete shuffle" though. An "incomplete shuffle", maybe :)

I've added a bit to my shuffling function to ensure a derangement. It's a bit quick and dirty...
sub gen_random_key{ ####sub fisher_yates_shuffle { my @array = split ("", $alpha); my @alpha = @array; for (my $i = @array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @array[$i, $j] = @array[$j, $i]; } for (0..$#alpha){ $decrypt{$alpha[$_]} = $array[$_]; $encrypt{$array[$_]} = $alpha[$_]; } #ensure a derangement for (0..$#alpha){ if ($alpha[$_] eq $array[$_]) { gen_random_key(); #warn 'key not deranged, setting another..'; return; } } }

Replies are listed 'Best First'.
Re^3: quick and dirty cryptogram puzzle
by runrig (Abbot) on Jan 02, 2008 at 18:09 UTC
    next if $i == $j;
    This line is unnecessary. It's been discussed before here and elsewhere, but in an array of any significant size (>10 or 20 or so), the odds that i and j are equal are low enough that the logic to test if they are equal outweighs the cost of just swapping the array element with itself (i.e. you're testing to see if they're equal on every iteration, costing something on every iteration, where if you dont' test you'd hardly ever be swapping something with itself anyway).