From eb43d2e2d8c874fe71a3b0bae6bd3f5e64638bfd Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Fri, 1 Feb 2008 23:14:22 -0500 Subject: implement random_string_from, which returns a string containing random characters from some character list --- lib/Games/Word.pm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib/Games/Word.pm') diff --git a/lib/Games/Word.pm b/lib/Games/Word.pm index 3aae86c..7b281f5 100644 --- a/lib/Games/Word.pm +++ b/lib/Games/Word.pm @@ -3,7 +3,8 @@ package Games::Word; require Exporter; @ISA = qw/Exporter/; @EXPORT_OK = qw/random_permutation is_permutation all_permutations - shared_letters shared_letters_by_position/; + shared_letters shared_letters_by_position + random_string_from/; use strict; use warnings; @@ -89,6 +90,17 @@ sub shared_letters_by_position { return wantarray ? @letters : grep { defined } @letters; } +sub random_string_from { + my ($letters, $length) = @_; + + die "invalid letter list" if length $letters < 1 && $length > 0; + my @letters = split //, $letters; + my $ret = ''; + $ret .= $letters[int rand @letters] for 1..$length; + + return $ret; +} + =head1 NAME Games::Word - ??? -- cgit v1.2.3-54-g00ecf