# How to Code Fair Random Spawning (Avoid Clustering) in Scratch > Re-roll spawn x positions that land too close to the previous spawn. Learn how to build fair random spawning (avoid clustering) in Scratch with clean block architecture, pitfalls, and extensions. ## How the Mechanism Works (Minimal Example) Re-roll spawn x positions that land too close to the previous spawn. Understand which variable owns the state and when it resets on the green flag so you can reuse this pattern in any genre. **Fair Random Spawning (Avoid Clustering) core pattern** - Initialize state variables cleanly on green flag - Update fair random spawning (avoid clustering) inside your main game loop - Test boundary values (0, max, and stage edges) Try it: Fair Random Spawning (Avoid Clustering) runs smoothly without race conditions or lag. ## Extensions, Common Pitfalls & Practice Once the minimal version works, add one audio or visual feedback cue and test with a friend. - Common pitfall: forgetting to reset state variables on green flag causes bugs on the second play. - Practice challenge: turn fixed speed or delay numbers into variables so you can tune game feel quickly. - [All 60 Scratch Game Mechanisms](/en/mechanisms/): Explore more reusable game systems. - [How to Make Games in Scratch (240 Guides)](/en/how-to-make/): Apply this mechanism to a complete game. Updated: 2026-11-23 Author: [Takamasa Suzuki](https://scratch.gmnavi.jp/about/author/) Japanese Edition: https://scratch.gmnavi.jp/mechanisms/random-spawning/ [Scratch Daizen (English)](https://scratch.gmnavi.jp/en/)