A simple Markov Model will be used in this question. For more information about Markov Chains, see http://setosa.io/ev/markov-chains/.
Take a string. For this example, we will use the word:
reader
Now, for each character, take the characters that appear after each occurence of the character in the string. (`^`
represents the start of the string and `$`
represents the end)
`^` -> {'r'} # After the start of the string, there is an `r`.
'r' -> {'e', `$`} # After the first `r` (*r*eader), there is an `e`
# after the second (reade*r*), there is the end of the string.
'e' -> {'a', 'r'}
'a' -> {'d'}
'd' -> {'e'}
Now, starting from the start of the string, pick randomly from one of the characters in the next set. Append this character and then pick from the characters in its next set, and so on until you get to the end. Here are some example words:
r
rereader
rer
readereader
If a character appears after another character multiple times, it is more likely to be chosen. For example, in cocoa can
, after a c
, there is a two thirds of a chance of getting an o
and a one thirds chance of getting an a
.
'c' -> {'o', 'o', 'a'}
Challenge
Create a program that takes no input and outputs a random string generated using a Markov Chain, like above, where the input to the chain is the program's source.
- The program must have at least two characters, two of which must be the same (To prevent "boring" chains that only have one output)
- You can modify the model to use bytes instead of characters if you wish, but change "characters" to "bytes" in rule 1
- The program should output strings randomly with the expected frequency in theory
This is code-golf, so the shortest program wins!
^
and$
in quotes? it might make it more clear to take it out of quotes, or put them in backquotes. \$\endgroup\$