Skip to main content

Generation

  • Audience years 5 and up
  • Duration 20 min
  • Runs on grid, ledger, cutouts

Key idea: Language models generate text one word at a time by sampling the next word according to learned counts.

Used in My First Language Model and How AI writes stories and Under the hood and Shaping a model — the lessons that include this module.

Choose your method: this module works with a grid (paper and dice), a ledger (tally sheets and a cup of counters) or cutouts (printed tokens). Choose whichever suits your materials.

Use a pre-trained (hand-built) Bigram model A model that predicts the next word from one previous word. It's what you build in the fundamental modules: each row of your grid says what can follow a single word. View in glossary to generate new text through Weighted random sampling Choosing the next token with probability proportional to its count. A d10 roll against the counts does this, and so does letting your eye land on one of the matching cutouts, because common words appear on more of them. View in glossary .

You will need

  • your completed bigram model from Training
  • a d10 (or similar) for weighted sampling
  • pen and paper for jotting down the generated text

For each group of four or five:

  • your group’s ledger sheets: the ones you filled in during Training, or a pre-tallied picture-book set
  • counters in four colours (red, blue, green, yellow), at least as many of each as the biggest tally on your sheets
  • a paper cup to draw from, and a tub to tip it back into
  • pen and paper for writing down the generated text

The How AI writes stories printouts have five pre-tallied picture-book sets, a page of counters to print and cut if you don’t have plastic ones, and a one-page instruction sheet for each group.

For each pair (or group) of students:

  • the trained cutouts spread from Training (already laid out on a table)
  • pen and paper for writing down the generated text

The Materials page has ready-to-print token cutouts for several texts if you haven’t done the Training module yet. Each PDF starts with a student-facing instructions page that walks groups through the matching game.

Your goal

Generate new text from your bigram language model. Stretch goal: keep going and write a whole story.

Generate new text from your ledger sheets, one counter at a time. Stretch goal: keep going and write a whole story.

Generate new text by walking the spread: pick a starting word, find the cutouts whose previous-word box colour matches, choose one visually, and repeat using the next word you picked as your new starting point. Stretch goal: keep going and write a whole story.

Key idea

A Language model A system that predicts what text comes next, using patterns learned from training data. Your hand-built grid, a cutouts spread, a booklet, and a room holding search sheets are all language models. View in glossary proposes several possible next words along with how likely each is. Dice rolls pick among those options, and repeating the process word by word yields fluent text.

The cup does the maths. A row’s tally marks go into the cup as counters, one for every mark, so a word with more marks has more counters in the cup and is more likely to come out. Drawing a counter without looking is , with no dice and no percentages.

The chain grows like dominoes: write a word, find a Cutouts Printed cards, one per word pair in the text, each showing a previous word in a box and the word that followed it. Spread on a table they are the model: to generate, find a card whose boxed word matches your current word and read off its next word. Common pairs appear on more cards, so picking by eye samples in proportion. View in glossary whose previous-word box matches it, write that cutout’s next word, then hunt for the next match. The spread already encodes the weights: common previous words appear on more cutouts, and your eye naturally lands on next words that appear more often. So scanning for matching cutouts and picking one visually is weighted random sampling, with no dice required.

Algorithm

  1. Choose a starting word from the first column of your Grid A sheet of grid paper with the vocabulary written down the side and across the top. Training fills each cell with tally marks for how often the row's word was followed by the column's word; generation reads a row and rolls against it. It's the original LLMs Unplugged apparatus. View in glossary .
  2. Look at that word’s row to find all possible next words and their counts.
  3. Roll dice weighted by the counts.
  4. Write down the chosen word and make it your new starting word.
  5. Repeat from step 2 until you hit a natural stopping point (e.g., .) or your desired length.
  1. Write down a starting word.
  2. Find the row for the current word (each sheet’s header shows the alphabetical run of words it holds).
  3. Fill the cup: one counter for every tally mark, each in the colour of its word’s box.
  4. Draw one without looking. Its colour names a box on the row, and the word in that box is your next word.
  5. Write it down: it’s the new current word. Tip the cup back into the tub, and pass the job to whoever has that word’s row.
  6. Repeat from step 2 until you reach a natural stopping point (e.g., .) or your desired length. A row with only one word on it needs no draw: just write that word down.

It’s a colour-matching game: the colour of the word you just wrote is the colour of the previous-word box you look for next.

  1. Pick a starting word: choose any word that appears as a previous word on at least one cutout
  2. Find candidates: scan the spread for cutouts whose previous-word box matches your current word’s colour, then verify the word itself matches before committing (two unrelated words can occasionally share a colour)
  3. Pick one cutout: visually pick any matching cutout (your eye will tend to land on cutouts whose next words are common, because there are more of them, which is weighted sampling for free)
  4. Write down the next word from the cutout you picked
  5. Put the cutout back in the spread (this matters: removing it would change the model’s distribution for next time)
  6. The word you just wrote becomes your new current word, so go back to step 2
  7. Keep going as long as you like; if no cutouts match your current word, just pick a new starting word and carry on. Stop when you’ve generated enough text

Example

Using the same bigram model from the example in Training:

Token see spot run . jump ,
see  ||    
spot   | |||
run    || |
. | | | 
jump    || |
,  ||| | 
  • choose (for example) see as your starting word
  • see (row) → spot (column); it’s the only option, so write down spot as next word
  • spot → run (25%), jump (25%) or , (50%); roll dice to choose
  • let’s say dice picks run; write it down
  • run → . (67%) or , (33%); roll dice to choose
  • let’s say dice picks .; write it down
  • . → see (33%), run (33%) or jump (33%); roll dice to choose
  • let’s say dice picks see; write it down
  • see → spot; it’s the only option, so write down spot… and so on

After the above steps, the generated text is “see spot run. see spot”

Using the ledger from the example in Training, with the row for spot and its cup:

,
spot
red
run
blue
jump
green
yellow
.
see
red
run
blue
jump
green
yellow
jump
.
red
,
blue
green
yellow
run
.
red
,
blue
green
yellow
see
spot
red
blue
green
yellow
spot
run
red
jump
blue
,
green
yellow
4 counters, one per mark
  • start with see and write it down
  • the row for see has one word on it, spot; there’s nothing to draw, so write down spot
  • the row for spot has run (1 mark, red), jump (1, blue) and , (2, green), so one red, one blue and two green counters go in the cup. Green is the likeliest, two chances in four, but let’s say red comes out: write down run and tip the cup back into the tub
  • the row for run has . (2, red) and , (1, blue); three counters, and let’s say red comes out, so write down .
  • the row for . has see, run and jump, one mark each; one red, one blue and one green counter, and let’s say red: write down see
  • back to the row for see: write down spot… and so on

After the above steps, the generated text is “see spot run. see spot”

Red meant run on the row for spot and . on the row for run. Because a colour names a box position on the current row, one set of counters works for every row and every book.

Using the cutouts spread from the example in Training:

Previous word Next words
see spot spot
spot run jump , ,
run . , .
. see run jump
jump . , .
, spot run spot jump
  • start with see and write it down
  • scan for cutouts with see as the previous word (look for boxes in see’s colour); both have spot as the next word, so pick one and write down spot
  • scan for spot cutouts: 4 matches (1 run, 1 jump, 2 ,). Pick visually. Your eye is more likely to land on a comma because there are two of them, but for variety let’s say run; write it down
  • scan for run cutouts: 3 matches (2 ., 1 ,). Two-thirds chance of .; let’s say . and write it down
  • scan for . cutouts: 3 matches (one each of see, run, jump). Let’s say see and write it down
  • back to scanning for see cutouts: both are spot, so write down spot… and so on

After the above steps, the generated text is “see spot run. see spot”

The randomness comes from your eye landing somewhere on the spread, so you don’t need dice. Previous words with more cutouts of the same next word are more likely to produce that word: both see cutouts have spot as the next word, so generation from see always produces spot.

Optional extension: pre-grouped piles

If you grouped the cutouts into piles during training (see the Training module’s optional extension), generation becomes faster: step 2 is now “find the pile labelled with my current word” instead of scanning the whole table. The behaviour and the resulting output distribution are unchanged.

Instructor notes

Icebreaker question

Before walking students through the algorithm, ask:

  • in as much detail as you can, explain what happens after typing something into the Chatbot (ChatGPT, Claude, Gemini) A product that wraps an LLM in a conversation. ChatGPT is OpenAI's, Claude is Anthropic's, Gemini is Google's, and there are many others. On this site we often name ChatGPT or Claude as shorthand for any of them: the concepts apply equally to every modern LLM chatbot, whichever one you use. View in glossary prompt box to produce the answer you get back

This surfaces students’ existing mental model of generation before they perform it by hand.

Discussion questions

  • how does the starting word affect your generated text?
  • why does the text sometimes get stuck in loops?
  • if this is a bigram (i.e. 2-gram) model, how would a unigram (1-gram) model work?
  • how could you make generation less repetitive?
  • does the generated text capture the style of your training text?
  • how does the starting word affect your generated text?
  • why does the cup have to be emptied between draws?
  • what happens on a row with only one word on it?
  • two groups with the same sheets: why aren’t their stories the same?
  • does the generated text sound like the original training text? Which bits are new?
  • how does the starting word affect your generated text?
  • why might the text sometimes get stuck repeating the same pattern?
  • what happens when only one cutout matches your current word?
  • why do we leave the cutouts on the table after picking one?
  • does the generated text sound like the original training text?

Troubleshooting

  • “Every row only has one tally mark: there’s nothing to roll for.” If the group didn’t get very far in Training and no row has more than one tally mark, there will only ever be one option for the next word and they’ll be stuck on rails. Have them either train on a bit more text or add extra tally marks to the grid wherever they like, which is an example of using Synthetic Data.
  • “I landed on a word that doesn’t have its own row.” This happens when a word only ever appeared as the last word in the training text, so it has a column (other words lead to it) and no row. Pick any other word that does have a row and continue from there.
  • “We’re stuck in a loop.” With small models it’s common to bounce between two words that only point at each other (e.g. , → spot → , → spot → …). Pick a different starting word, or choose any other valid next word to break out of the cycle.
  • “The counters from the last row are still in the cup.” Tip the cup back into the tub after every draw. Leftover counters change the odds, and can name a box the current row doesn’t have.
  • “This word’s row has no marks.” The word only ever ended the text, so nothing ever came after it. Start again from any word.
  • “Which box is this colour?” The boxes always run red, blue, green, yellow from the left, and the printed sheets name the colour in the corner of each box.
  • “Everyone’s waiting on one person.” Give the group four jobs: a reader who finds the row, a filler who loads the cup, a drawer, and a writer.
  • “Every previous word only has one matching cutout: there’s nothing random about this.” If the spread is small and each previous word only matches a single cutout, generation is deterministic. Either train on more text, or add a few extra cutouts written out by hand, which is an example of Synthetic Data.
  • “I picked a next word that doesn’t appear as a previous word anywhere on the table.” This happens when a word only ever appeared as the last word in the training text, so it shows up as a next word and never as a previous one. Pick a different starting word that does appear on the spread and continue from there.
  • “We keep going back and forth between the same two words.” With small spreads it’s common to get stuck in a loop where two words keep pointing to each other. Start from a different word, or pick any other matching cutout to break the cycle.

Connection to current LLMs

This Generation Using a trained model to produce new text by repeatedly predicting and choosing the next token. Look up the current word, pick a next word in proportion to the counts, write it down, repeat. View in glossary process is identical to how current LLM (Large Language Model) A language model trained on a very large amount of text, with billions of parameters. The hand-built models in these lessons are tiny language models; ChatGPT, Claude and Gemini are large ones, built on the same principles at a far greater scale. View in glossary produce text:

  • sequential generation: both generate one word at a time
  • probabilistic sampling: both use weighted random selection (exactly like your dice, cup or cutouts spread)
  • Probability distribution A set of options with a likelihood attached to each. The counts in one row of your grid, the matching cutouts on a spread, or the show of hands when the room is asked "who has `the`?" are all probability distributions over the next word. View in glossary : Neural network A computational system, loosely inspired by biological neurons, that learns patterns from data by adjusting numerical weights. Modern LLMs are very large neural networks. Your hand-built model is not one, but it stores the same kind of information: which words tend to follow which. View in glossary outputs probabilities for all 50,000+ possible next Token A single unit of text that the model works with. In our activities each word and punctuation mark is a token. Modern LLMs use subword tokens, so a long or unusual word may be split into several. View in glossary
  • no planning: neither looks ahead, it just picks the next word
  • variability: same Prompt The input text you give a language model. In a bigram model the prompt is just the current word. In a modern LLM a prompt can run to hundreds of thousands of tokens, which gives the model far more context to work with. View in glossary can produce different outputs due to randomness

Repeat that loop thousands of times on a model trained on enough text and you get a full reply. And because each step is a random draw, asking for a response twice gives two different answers.

In AI and machine learning more broadly, using a trained model to produce outputs is called “ Inference Using a trained model to produce outputs. In language models, inference means generating text. These lessons say "generation" because it describes what language models do more clearly, but "inference" is the word you'll meet in AI/ML literature and tooling. View in glossary ”. These resources use “generation” because it states directly what language models do: they generate text.

Comparison to dice method

The cup and the dice method produce equivalent results:

  • dice rolls with weighted probabilities select from options based on counts
  • the cup holds one counter per count, so a draw selects from the same options with the same weights
  • the cup makes the probability tangible: if the row for the has four marks beside cat and one beside dog, four of the five counters name cat, and you can see and feel which is likelier before you draw

The cup method avoids percentages and dice ranges, which is why it suits primary and junior secondary classes.

Comparison to dice method

The cutouts spread and the dice method produce equivalent results:

  • dice rolls with weighted probabilities select from options based on counts
  • visual selection from the spread selects from options where counts are represented by multiple physical cutouts
  • the spread makes the probability tangible: if three of the cutouts with the as the previous word have cat as their next word and one has dog, you’ll land on cat about 75% of the time, just as weighted dice would

The cutouts method avoids percentages and dice ranges, which suits younger classes.

Interactive widget

Step through the generation process at your own pace. Click on a row to select a starting word, then press Play or Step to watch the dice roll and text being generated. You can also edit the training text to create your own model.

Training text
Training text (tokenised)
thecatsatonthemat.
Model
Tokenthecatsatonmat.
​|​​|​
​​|​​​
​​​|​​
|​​​​​
​​​​​|
.​​​​​​
Output

The generation widget samples from the same model with a d10: choose Grid above to step through it.

Step through the generation process at your own pace. Click on a cutout to select a starting word, then press Play or Step to watch matching cutouts being picked and text being generated. You can also edit the training text to create your own model.

Training text
Model
the
catmat
cat
sat
sat
on
on
the
mat
.
Output