Markov Chain Algorithm in Perl
Yu-Chung Chau
CS 265
Markov Chain Origins
Andrey Markov (1856-1922)
Russian mathematician
Notable for theory on
stochastic (random) processes,
later known as Markov chains
Many real world applications
Markov Text Generator
Generate superficial “real-looking” text using sample
document
Mark V Shaney
Used by spammers to inject “real-looking” hidden
paragraphs into emails
Markov Chain Algorithm in Perl
$MAXGEN = 10000;
$NONWORD = "\n";
while (<>) {
foreach (split) {
push(@{$statetab{$w1}{$w2}}, $_);
($w1, $w2) = ($w2, $_);
}
}
push(@{$statetab{$w1}{$w2}}, $NONWORD);
$w1 = $w2 = $NONWORD;
for ($i = 0; $i < $MAXGEN; $i++) {
$suf = $statetab{$w1}{$w2};
$r = int(rand @$suf);
exit if (($t = $suf->[$r]) eq $NONWORD);
print "$t\n";
($w1, $w2) = ($w2, $t);
}
$w1 = $w2 = $NONWORD;
Markov Chain Algorithm in Perl
Two Phases
1. Build
2. Generate
Initialization
$MAXGEN = 10000;
#max words to generate
$NONWORD = “\n”;
#non-word variable
$w1 = $w2 = $NONWORD; #initializing prefix variables
Markov Chain Algorithm - Build
…
while (<>) {
foreach (split) {
push(@{$statetab{$w1}{$w2}}, $_);
($w1, $w2) = ($w2, $_);
}
}
push(@{$statetab{$w1}{$w2}}, $NONWORD);
…
Markov Chain- Behind the Build
Building Word List
“It is a good day to die. It is a nice day to live.”
%statetab = (
);
‘It is‘
‘is a’
‘a good ’
‘good day’
‘day to’
…
=>
=>
=>
=>
=>
[a, a],
[good, nice],
[day],
[to],
[die., live.],
Markov Chain Algorithm - Generate
…
for ($i = 0; $i < $MAXGEN; $i++) {
$suf = $statetab{$w1}{$w2};
$r = int(rand @$suf);
exit if (($t = $suf->[$r]) eq $NONWORD);
print "$t\n";
($w1, $w2) = ($w2, $t);
}
Markov Chain – Behind the Generate
%statetab = (
‘ It is ‘
‘ is a ’
‘ a good ’
…
=>
=>
=>
[a, a],
[good, nice],
[day],
);
Original:
“It is a good day to die. It is a nice day to live.”
Sample Output:
“It is a nice day to die. It is a good day to die.”
The End
“I spent an interesting evening recently with a grain of salt”
- Mark V Shaney
© Copyright 2026 Paperzz