Lesson 01-03

Random Choice for Generation

12 min
1 export
3 tests

Lesson blocked by prerequisites

Complete and save a passing attempt for your active lesson before running this one.

Go to active lesson

Lesson workspace sections

Submission + Results
Not run yet

Seed 103 • Runtime includes 2 prerequisite modules
Test Results

Run tests to see case-by-case feedback.

Attempts0 saved

No saved attempts yet.

Lesson README

01-03 Random Choice for Generation

Why this matters

Weighted sampling turns static token probabilities into diverse generation behavior.

Intuition first (no jargon)

Higher-probability tokens should be selected more often, but not deterministically every time.

Code walkthrough

js
export function weightedRandom(items, weights, rng = Math.random) {
  // return one item based on weights
}

Your task

Implement weightedRandom(items, weights, rng).

  • Validate equal lengths and non-empty arrays.
  • Treat weights as non-negative.
  • Use cumulative sums and one random draw.

Hints

  • Sum weights first.
  • Draw r = rng() * total.
  • Walk cumulative weight until r < cumulative.

Check your thinking

  1. Why pass rng as an argument?
  2. What happens if all weights are zero?
  3. Why do tests prefer deterministic randomness?

Stretch (optional)

Handle tiny floating-point errors robustly when selecting the last item.

Likely test focus

  • Returns only from items.
  • Deterministic with seeded rng.
  • Correct behavior on edge inputs.

What should improve

Your generator can now sample plausible alternatives instead of always taking argmax.

Bridge to next lesson

Next lesson: combine cleaning, counting, and sampling into one baseline generator.

Monaco Editor

Matches starter

Files

Editor is deferred on smaller screens to keep startup fast.

Autosave is enabled in local storage for this lesson.