Loading...
beginner
Problem 03 • Step 02
Separate the Random
In the last step, you found the bug by running the function many times in a loop. That works, but it is slow and fragile. A rare bug might slip through.
The problem is that randomChoice mixes two concerns:
- Generating a random number in a range (hard to test precisely)
- Using that number to pick from a list (trivial once you have the number)
Your task
Split them apart:
randomInRange(max): returns a random integer from 0 up to (but not including)maxrandomWord(list): usesrandomInRangeto pick a random item from the list
randomInRange is still random, but its contract is deterministic and testable: the result must always be >= 0 and always < max. The starter tests already verify this. Extend them to cover randomWord too.
This is the key insight: when something is hard to test, do not test harder. Separate the parts so each one is easy to test on its own.