# Generation

> Use your hand-built bigram model to generate new text through weighted random sampling.

<VariantToggle client:load options={["grid", "ledger", "cutouts"]} />

Use a pre-trained (hand-built) <GlossaryTerm id="bigram-model">bigram model</GlossaryTerm> to generate new text through <GlossaryTerm id="weighted-random-sampling">weighted random sampling</GlossaryTerm>.

<Variant is="grid">

![](../../assets/images/hero-grid-generation.avif)

</Variant>

<Variant is="cutouts">

![](../../assets/images/hero-cutouts-generation.avif)

</Variant>

## You will need

<Variant is="grid">

- your completed bigram model from [Training](https://www.llmsunplugged.org/modules/training/)
- a d10 (or similar) for weighted sampling
- pen and paper for jotting down the generated text

</Variant>

<Variant is="ledger">

For each group of four or five:

- your group's ledger sheets: the ones you filled in during
  [Training](https://www.llmsunplugged.org/modules/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](https://www.llmsunplugged.org/lessons/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.

</Variant>

<Variant is="cutouts">

For each pair (or group) of students:

- the trained cutouts spread from [Training](https://www.llmsunplugged.org/modules/training/) (already laid
  out on a table)
- pen and paper for writing down the generated text

The [Materials](https://www.llmsunplugged.org/tools/#cutouts) 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.

</Variant>

## Your goal

<Variant is="grid">

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

</Variant>

<Variant is="ledger">

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

</Variant>

<Variant is="cutouts">

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.

</Variant>

## Key idea

<Variant is="grid">

A <GlossaryTerm id="language-model">language model</GlossaryTerm> 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.

</Variant>

<Variant is="ledger">

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 <GlossaryTerm id="weighted-random-sampling">weighted random sampling</GlossaryTerm>, with no dice and no percentages.

</Variant>

<Variant is="cutouts">

The chain grows like dominoes: write a word, find a <GlossaryTerm id="cutouts">cutout</GlossaryTerm> 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.

</Variant>

## Algorithm

<Variant is="grid">

1. Choose a starting word from the first column of your <GlossaryTerm id="grid">grid</GlossaryTerm>.
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.

</Variant>

<Variant is="ledger">

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.

</Variant>

<Variant is="cutouts">

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

</Variant>

## Example

<Variant is="grid">

Using the same bigram model from the example in [Training](https://www.llmsunplugged.org/modules/training/):

<LmGrid tokens="see spot run . see spot jump . run , spot , run . jump , spot , 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"_

</Variant>

<Variant is="ledger">

Using the ledger from the example in [Training](https://www.llmsunplugged.org/modules/training/), with the
row for `spot` and its cup:

<LmLedger
  tokens="see spot run . see spot jump . run , spot , run . jump , spot , jump ."
  focus="spot"
  cup="spot"
/>

- 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.

</Variant>

<Variant is="cutouts">

Using the cutouts spread from the example in [Training](https://www.llmsunplugged.org/modules/training/):

<LmCutouts tokens="see spot run . see spot jump . run , spot , run . jump , 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`.

</Variant>

<Variant is="cutouts">

## 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.

</Variant>

## 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 <GlossaryTerm id="chatbot">ChatGPT or Claude</GlossaryTerm> 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

<Variant is="grid">

- 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?

</Variant>

<Variant is="ledger">

- 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?

</Variant>

<Variant is="cutouts">

- 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?

</Variant>

### Troubleshooting

<Variant is="grid">

- **"Every row only has one tally mark: there's nothing to roll for."** If the
  group didn't get very far in [Training](https://www.llmsunplugged.org/modules/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](https://www.llmsunplugged.org/modules/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.

</Variant>

<Variant is="ledger">

- **"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.

</Variant>

<Variant is="cutouts">

- **"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](https://www.llmsunplugged.org/modules/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.

</Variant>

### Connection to current LLMs

This <GlossaryTerm id="generation">generation</GlossaryTerm> process is identical to how current <GlossaryTerm id="llm">LLMs</GlossaryTerm> 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)
- **<GlossaryTerm id="probability-distribution">probability distribution</GlossaryTerm>**: <GlossaryTerm id="neural-network">neural network</GlossaryTerm> outputs probabilities for all
  50,000+ possible next <GlossaryTerm id="token">tokens</GlossaryTerm>
- **no planning**: neither looks ahead, it just picks the next word
- **variability**: same <GlossaryTerm id="prompt">prompt</GlossaryTerm> 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.

<Variant is="grid">

In AI and machine learning more broadly, using a trained model to produce
outputs is called "<GlossaryTerm id="inference">inference</GlossaryTerm>".
These resources use "generation" because it states directly what language
models do: they generate text.

</Variant>

<Variant is="ledger">

### 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.

</Variant>

<Variant is="cutouts">

### 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.

</Variant>

## Interactive widget

<Variant is="grid">

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.

<GenerationWidget client:visible />

</Variant>

<Variant is="ledger">

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

</Variant>

<Variant is="cutouts">

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.

<CutoutsGenerationWidget client:visible />

</Variant>

Source: <https://www.llmsunplugged.org/modules/generation/>\
Licence: [CC-BY-SA-4.0](https://creativecommons.org/licenses/by-sa/4.0/)\
Cite as: Swift, B. (2026). _LLMs Unplugged: Understand how AI language models work by building one yourself._ https://doi.org/10.5281/zenodo.17403824
