Spielerando

Pick a random living enemy without building a list every frame

Games often need one random thing out of those that fit a condition, such as one enemy in range or one free spawn point. Building a filtered list each time wastes work, and picking a random index first can land on something that does not fit. One walk over the items does it fairly.

Why this works

You can choose fairly without knowing how many there will be. Let each new match take the place of the current choice with a chance that shrinks as the count grows, and by the end every match has had exactly the same chance, with no list built and no retries.

When not to use it

Skip it when you need several picks, or the same list is searched many times in a row. Then build the list once and use it, which is clearer. Also skip it for tiny groups, where the saving is too small to be worth the extra reading.

In the full entry

  • What to build, step by step
  • Starting values and the reason for each
  • 3 common mistakes and the fix
  • 24 lines of example code with 3 checks that pass
  • How your AI shows you that it works

Say this to your AI

Use the Spielerando entry "Pick a random living enemy without building a list every frame" (read_entry pick-one-random-match-in-a-single-pass) in my game. Build it step by step, and show me how you checked that it works.

Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP