I'm building a coin-flipping example where every five-flip sequence must contain exactly four Heads and one Tail, but the Tail should appear in a random position. I'd like to understand and implement the logic myself rather than simply relying on a ready-made weighted-random library. What algorithm or step-by-step approach should I use?
3 Answers
There are two separate problems here: creating randomness and arranging the fixed outcomes. Fisher–Yates handles the arrangement, but it still needs a random index at each step. If you avoid the language’s random-number library, you’ll need to implement a pseudorandom number generator and seed it somehow. For learning, it’s usually better to use the built-in generator first and focus on understanding the shuffle.
Start by describing the process in plain language: create five slots, put four Heads and one Tail into them, choose positions to swap using random indexes, and repeat until the collection is shuffled. Then translate each step into code and test it many times. Check that every output has four Heads and one Tail, and count how often the Tail appears in each position. With a fair shuffle, the five positions should occur at roughly similar rates over a large number of trials.
Treat the required results as a collection containing [Heads, Heads, Heads, Heads, Tail], then shuffle that collection. A Fisher–Yates shuffle is a good algorithm to study and implement yourself. After shuffling, read the elements from left to right. Every result will contain exactly four Heads and one Tail, while each position can vary.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically