Minecraft Commands – How to Target a Random Entity Using @r

minecraft-commandsminecraft-java-edition

While @r alone can only target player entities, adding the type parameter allows it to target other entity types:

/say @r[type=ArmorStand]

But because of this requirement, you'd only be able to target all except for a specific entity:

/say @r[type=!Player]

Attempting to use an invalid ID will throw an error, stating "Entity type [name] is invalid", rather than targeting all except that invalid type.

/say @r[type=!InvalidID]

How would I cause @r to target all possible entities without excluding any?

Best Answer

In newer versions of Minecraft, you can set the sort mode that will choose how targets are sorted. You can then limit the number of targeted entities to 1, so that you are returned only 1 entity.

Here is a command that lets you do that:

/say @e[sort=random,limit=1]

The list of targets is sorted randomly, and then the limit being 1 means only the 1st one of that list is selected, essentially giving you a random entity selection.