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
jsexport 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
- Why pass
rngas an argument? - What happens if all weights are zero?
- 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.