Markov Chain Text Generator, Explained
After reading this you will know how a Markov chain turns a block of text into a table of counts, how it samples new text from those counts, and how to read the entropy and context readouts to tell whether the model is inventing or just copying.
What a Markov chain does with text
A Markov chain over text is a counting machine. It reads the training text as a sequence of tokens, and for every window of k consecutive tokens (the context) it records which token came next and how often. Generation reverses the process: start from some context, look up its continuation counts, pick a next token in proportion to those counts, slide the window forward by one, and repeat.
Here is the hook. Train an order-2 character chain on the word banana repeated a few times. The context an is always followed by a, and na is always followed by n until the end. The chain will happily emit ananananana forever, because at each step it only sees the last two characters and never remembers that a real word ends. That single fact, that the model sees nothing beyond its k-token window, explains almost everything the tool does well and everything it does badly.
When to use it and when not to
Reach for a Markov chain when you want to understand the mechanics of a generative model from counts you can verify by hand. It is exact, fast, and fully inspectable. Nothing is hidden inside learned weights. It is also a fine tool for cheap text effects: name generators, placeholder prose, glitchy remixes of a source document.
Do not use it when meaning, long-range structure, or novelty matter. An order-3 word chain trained on a novel will produce grammatical-looking fragments that are mostly verbatim copies of the source, because most three-word contexts appear exactly once. It cannot balance a quote or a bracket opened ten tokens ago, cannot stay on topic, and cannot say anything the source did not already say in a short local window. Those are the exact limitations that neural language models exist to address.
The tool runs entirely in your browser. Training is just counting, so even a few thousand tokens retrain in milliseconds and you can change k or switch between character and word units without any server round trip.
The formula and the intuition behind it
Let a context be a sequence of k tokens, written c = (t_{i-k}, \dots, t_{i-1}). Let N(c, w) be the number of times token w follows context c in the training text, and let N(c) be the total over all continuations. The model estimates the probability of each next token as a simple ratio.
Here P(w \mid c) is the probability of continuation w given context c, N(c, w) is the count of that specific continuation, and the denominator sums the counts of every token seen after c. This is the maximum-likelihood estimate: the values that make the observed training text most probable are exactly the observed frequencies. No smoothing, no priors, just division.
To measure how much choice a context offers, use the Shannon entropy of its continuation distribution.
H(c) is measured in bits. A context with one continuation has H(c) = 0: no choice, pure copying. A context with two equally likely continuations has H(c) = 1 bit. The tool's entropy readout is the count-weighted average of H(c) across all contexts, so it tells you how much freedom the chain has, averaged over how often each context is actually visited.
A worked example on the preloaded text
The tool preloads a public-domain excerpt of Alice in Wonderland. To keep the arithmetic checkable by hand, work a tiny slice of it as an order-2 character chain. Take the string the cat sat (11 characters including spaces).
Counting contexts and continuations for "the cat sat"
- Slide a 2-character window across the string and record the character that follows each window. The context
atappears twice: once incatfollowed by a space, once at the end ofsatwhere nothing follows. - For the interior occurrence,
atis followed by a space. So the counted continuations foratare: space, count 1. That gives N(\text{at}) = 1. - Look at context
t(t then space). It appears once incat sat, followed bys. So P(s \mid \text{"t "}) = 1/1 = 1. - Now take a context with real choice. Train instead on
the cat sat on the mat. The contextatnow appears three times:cat,sat, andmat(end). Its two counted continuations are space (count 2) and end (count 1 if you track end, or count 2 with no continuation otherwise). - Restrict to the two space-followed cases so the numbers stay clean. Then P(\text{space} \mid \text{at}) = 2/2 = 1, entropy 0. The context is deterministic.
The lesson from the arithmetic: on short training text almost every context has one continuation, so entropy sits near zero and the chain has no choice to make. It replays the source.
The chart below shows top continuations for the context he on a slightly larger character model trained on the full opening paragraph, where he is followed by a space, by r (in "her"), and by n (in "when"). The bars are the maximum-likelihood probabilities.
How order k reshapes everything
The single most instructive parameter is the order k. Raise it and the text reads better but copies more. The mechanism is countable. As contexts get longer, each specific context appears fewer times in the training text, so more of them have exactly one continuation, so entropy drops toward zero and the chain replays verbatim spans.
The table gives representative numbers for a character model on the full Alice excerpt. Your exact values depend on the loaded text, but the direction never changes.
| order k | distinct contexts | mean continuations | mean entropy (bits) |
|---|---|---|---|
| 1 | 34 | 8.2 | 3.05 |
| 2 | 210 | 3.4 | 1.72 |
| 3 | 640 | 1.9 | 0.83 |
| 4 | 1180 | 1.4 | 0.41 |
| 6 | 1990 | 1.1 | 0.14 |
Reading the readouts
Three numbers tell you the state of the model.
- Distinct contexts
- How many unique
k-token windows appear in the training text. This grows quickly withkand with vocabulary size. A word model with order 3 on a novel can have tens of thousands of contexts. - Mean continuations per context
- The average of N(c) counted as distinct next tokens. When this drops near 1, the chain has little choice and mostly copies.
- Mean entropy per step
- The count-weighted average of H(c). Near zero it is a warning: the model has become a lookup table of the source and generation will reproduce long verbatim runs.
Use the inspector to check a single context directly. Type a prefix and read the bars. If one bar is at 1.0, that context is deterministic and contributes 0 bits. If four bars sit near 0.25 each, that context contributes close to 2 bits and is a genuine branching point.
Common mistakes
Do not read low entropy as a sign the model is good. Entropy near 0 means the model is copying, not that it learned language. A high-order chain that scores 0.1 bits per step is echoing the source almost word for word.
Three traps catch people often. First, confusing coherence with learning: an order-5 word chain looks fluent because it is quoting the source, not because it understands grammar. Second, training on too little text: with a short document even order 2 collapses to near-deterministic transitions, so you get one output on repeat. Third, expecting balanced structure: since the chain sees only k tokens, an open quotation mark or parenthesis is forgotten within k steps and will rarely be closed correctly. That failure is structural, not a bug you can tune away.
One more numeric check. If mean continuations per context is 1.0 exactly, every context is deterministic and generation from a given seed is fixed. Any variety you see then comes only from the random starting context, not from sampling.
Related tools
A Markov chain generates by counting explicit transitions. Other generative models learn continuous distributions instead. The Diffusion Model Toy trains a tiny denoiser that turns pure noise back into a 2D shape, a different route to sampling new data. The GAN Trainer (2D) pits a generator against a discriminator and lets you watch mode collapse happen live, which is the neural analogue of a Markov chain getting stuck replaying one span.
Frequently asked questions
What order k should I use?
For characters, start at 2 or 3. Order 1 is near noise, order 5 and up mostly copies. For words, order 1 or 2 is usually enough because word contexts are already specific. Watch the mean entropy readout: if it drops below about 0.5 bits, you are in copy territory.
Why does it sometimes repeat the same phrase forever?
Because a cycle of deterministic contexts exists. If an always leads to a and na always leads to n, the chain loops. This happens whenever a set of contexts each has a single continuation that points back into the set.
Is character mode or word mode better?
Character mode invents new word-like strings and needs less text to feel varied. Word mode produces real words and reads more smoothly but copies sooner because word contexts are rarer. Neither is better in general; they answer different questions.
Why are the probabilities just counts divided by a total?
Because that is the maximum-likelihood estimate. Among all possible probability values, the observed frequencies are the ones that assign the highest probability to the training text you actually saw. No extra assumptions are added.
Can a Markov chain produce something not in the training text?
At the token level, yes: it can stitch together a continuation from context A onto text from context B, producing a sequence that never appeared. At the span level, high-order chains produce almost nothing new because their contexts are unique. Lower k for more novelty, higher k for more fidelity.