|Just another Perl shrine|
RE: Cryptogram Generatorby btrott (Parson)
|on Feb 07, 2000 at 23:30 UTC||Need Help??|
A couple of comments:
1) the open || die statement should actually be
For reasons of precedence.
2) Here are some faster algorithms. One way is to use a hash to record which characters you've already placed into your substitution string. Here's the main loop:
This way you don't have to do a search through the string each time.
And here's an even better way of doing it: build a random permutation of the alphabet string. To do this, we'll actually need an array:
Now we set another array equal to this one, then call the random shuffle algorithm on it:
which shuffles the array in place. Now all you need to do is get back the substitution string:
Here's the definition of the fisher_yates_shuffle sub:
(Taken directly from perlfaq4.)
I did some benchmarking on these, and here's what I got:
where "orig" is the one you posted, "f_yates" is the one using fisher_yates_shuffle, and "hash" is the one using a hash to record seen characters.
If I've messed anything up, let me know.