Skip to content
Tutorials Center
My learning

Topics

Lesson 79 of 86 ยท Projects

Project: Random Question Generator

Build a JavaScript generator that creates percentage, ratio, time-and-work and age questions with clean answers, distractors and an independent checker.

A fixed set of practice questions runs out quickly, and once you remember the answers the practice stops being practice. A question generator solves that: it creates fresh questions from templates and random numbers, so you can drill the same skill as many times as you like. It is also a genuinely good test of your own understanding, because to generate a question you must know exactly which numbers make the answer come out clean.

In this project you will write a generator for four familiar question types (percentages, ratio, time and work, and ages) in plain JavaScript. It has three parts: a seeded random number generator so that results are repeatable, one generator per topic that builds the question from the answer backwards, and an independent checker that re-solves every question a different way before it is accepted.

The pipeline

flowchart LR
    A["Seed"] --> B["Random number generator"]
    B --> C["Topic generator: pick numbers, build text and answer"]
    C --> D["Add distractor options"]
    D --> E["Independent checker re-solves the question"]
    E -->|"Same answer"| F["Accept question"]
    E -->|"Different answer"| G["Reject and report a bug"]

A question is only shown if a second, independent solver agrees with the first.

Seeded randomness

Math.random() gives a different sequence every time, which is awkward for a trainer: you cannot reproduce a question you want to discuss, and you cannot test your code reliably. A seeded generator turns a number (the seed) into a repeatable sequence. The same seed always gives the same questions, so a friend who uses seed 2025 sees exactly what you see.

The generator below is a small, well-known algorithm called mulberry32. You do not need to understand its bit arithmetic; what matters is the behavior.

JS A seeded random number generator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first two lines are identical because they come from the same seed. The ten bucket counts are all close to 1,000, as a fair generator should produce.

Build the question from the answer

The key idea of a good generator is design from the answer. If you simply choose random numbers, you get messy questions like "What is 12% of 347?", whose answer is 41.64. Exams avoid that. Instead, choose numbers that guarantee a clean result:

  • Percentage: choose the percentage p, then choose the base n as a multiple of 100 / gcd(p, 100). That makes n x p / 100 a whole number. For 12%, the unit is 25, so the base is a multiple of 25.
  • Ratio: choose coprime parts a and b (such as 3 and 4) and a multiplier k, then set the total to (a + b) x k. The shares are ak* and bk*.
  • Time and work: two workers finishing a job alone in a and b days take ab / (a + b) days together. Keep only pairs where that divides exactly, using a simple retry loop.
  • Ages: choose the son's age and the multiple k, then give the sum of the ages. The son's age is the sum divided by (k + 1), a whole number by construction.

Distractors

For multiple choice you also need three wrong options that look tempting. The best distractors are the typical mistakes: for ratio, the smaller share; for time and work, adding the days; for percentages, the complement (the rest after taking the percentage). The generator below stores such values in a wrongs list, tops up with neighbors if any are duplicates, and shuffles all four options.

The independent checker

If the generator and the checker use the same formula, a mistake in that formula passes silently. So the checker re-solves by a different route, usually brute force over small whole numbers or a simulation:

  • percentage: search for the whole number x with x x 100 = n x p
  • ratio: search for the share x where x : (total - x) = b : a
  • time and work: simulate days, counting work units (A does b units a day, B does a units a day, the job is ab units)
  • ages: search for the son's age s with s + k x s = sum

Brute force is slow for big numbers, but for questions with numbers under a few thousand it is instant and almost impossible to get wrong in the same way as the formula.

The generator

Run this program. It builds eight questions from seed 2025, checks each one with the independent solver and prints the results. At the end it tampers with an answer to show that the checker catches a wrong one. Change SEED to get a new set.

JS Question generator with checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Reading the code

  • generators returns the question text, the right answer, a list of tempting wrong answers, and the raw numbers (data) the checker needs.
  • checkers uses a different method for each topic. Because the checkers never call the generator's formulas, an error in one is unlikely to be repeated in the other.
  • makeOptions collects the answer and the distractors in a Set (which drops duplicates), adds neighbors of the answer if fewer than four are left, then shuffles with a Fisher-Yates loop driven by the same seeded generator, so the order is reproducible too.
  • Because the rng is passed in everywhere, the whole set depends only on SEED.

Ideas to extend it

  1. Add more topics: simple interest, profit and loss, speed and distance, averages. For each, think "what numbers make this clean?" and "what would a hasty student compute?"
  2. Plug the generator into the Timed Quiz Trainer so it asks fresh questions on every run.
  3. Add a difficulty setting that widens the number ranges or chains two steps, such as a percentage of a percentage.
  4. Record which topics you miss most often and generate more of those.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why do we use a seeded random number generator instead of Math.random()?

    A seed makes the sequence reproducible. You can share a seed, replay a question and write dependable tests.
  2. 2. To make 'what is p% of n' always give a whole number, what should n be a multiple of?

    n x p / 100 is whole exactly when 100 divides n x p, which happens when n is a multiple of 100 / gcd(p, 100). For p = 12 that unit is 25.
  3. 3. Two workers finish a job alone in 12 and 24 days. How many days do they need together, and does a brute-force simulation agree?

    The formula 12 x 24 / 36 gives 8. In units of the job, A does 2 and B does 1 each day (24 units total), so 3 units a day finish it in 8 days.
  4. 4. What is the main reason to write an independent checker for each question?

    If both parts shared one formula, a wrong formula would pass unnoticed. Solving by a different route, such as brute force, exposes it.
  5. 5. A generator picks the ratio 3 : 4 and a multiplier of 6. What total does it print, and what is the larger share?

    The total is (3 + 4) x 6 = 42, and the larger share is 4 x 6 = 24.

Summary

  • A seeded generator makes random questions repeatable, shareable and testable.
  • Build each question from the answer backwards so every number comes out clean.
  • Use typical mistakes as distractors and shuffle with the same seeded generator.
  • Verify every question with an independent checker that solves it a different way.
  • The same pattern extends to any topic where you can state what makes the numbers tidy.

Next: Project: Data Interpretation Dashboard, where you will turn one dataset into a table, a bar chart and a pie chart, and ask questions about it.