I’ve recorded how often some letters occur in a set of strings, and now I want to make some random strings that have (approximately) the same composition of letters. I’m using the following Perl code to do this.
my $random_string = "";
while(length($random_string) < $length)
{
my $probabilities =
{
A => 0.2790114613,
B => 0.1880372493,
C => 0.2285100287,
D => 0.3044412607,
};
my $test = 0;
$test += $probabilities->{ A };
if($rand < $test)
{
$sequence .= "A";
next;
}
$test += $probabilities->{ B };
if($rand < $test)
{
$sequence .= "B";
next;
}
$test += $probabilities->{ C };
if($rand < $test)
{
$sequence .= "C";
next;
}
$sequence .= "D";
}
Is there a better way of doing this? How can I handle cases when I don’t know how many letters there are to be considered? We can safely assume that the sum of probabilities for all the letters is 1.
Check List::Util::WeightedChoice.