I need a random number from -100 to 100 for a command block /tp @p (random number 1) 200 (random number 2)

  • Within the game? It's not clear from your post. – Joachim Nov 5 '19 at 16:09
  • I'm trying to get a to command in a command block that goes /tp @p (random) 200 (random). The 2 random numbers need to be different. I've edited the post – Robert Lucas Nov 5 '19 at 16:11
  • 1
  • 2
    Strongly disagree with this proposed duplication. This is a classic example of the XY problem. This question is not really asking how to generate a number, it is asking how to teleport a player to a random location within specified parameters. – Unionhawk Nov 5 '19 at 18:03

As far as I know, there is no way to directly generate a random number using commands, and there definitely isn't any way to then plug that random number into another command block.

However, the spreadplayers command will do what you're after here. spreadplayers is designed to place groups of, or individual players, in random spots around the map.

The syntax is as follows for Java Edition: /spreadplayers <x> <z> <spreadDistance> <maxRange> <respectTeams> <player>. <x> <z> specifies the center of the map, <spreadDistance> specifies the minimum distance between the targets, <maxRange> specifies the maximum horizontal distance to place players from the center, <respectTeams> specifies whether or not teams should be kept together, and <player> specifies your targets.

In the particular case of sending a single @p to some location between x,z (-100,-100) and (100,100), the command would look like so:

/spreadplayers 0 0 0 100 false @p
| improve this answer | |
  • I suspect what you're really after is spreading a bunch of players around the map to random spots, which can be accomplished by subbing in an @a to the final command here. – Unionhawk Nov 5 '19 at 16:13
  • If this works in 1.12 (which I didn't specify in the question, sorry) I'll mark this as correct – Robert Lucas Nov 5 '19 at 16:15
  • @RobertLucas It should, it was added in 1.6.1 and I don't see anything to suggest that it has been changed since then for Java Edition, so it should work in 1.12. – Unionhawk Nov 5 '19 at 16:16
  • It's for 1 person – Robert Lucas Nov 5 '19 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.