How spawn players randomly around a specific point

Discussion in 'Plugin Development' started by webbhead, Sep 15, 2015.

Thread Status:
Not open for further replies.
  1. Offline


    The title really says it all.... I am making a mini game and what the players to spawn in a random spawn around the Arenas spawn (I want to make it a 10x10 area they can spawn in) also I would like to know how to do it safely.
  2. Get the lowest corner Then do x += random.nextInt(10); (and the same for y and z) to get your random location
  3. Offline



    Im not using WorldEditAPI and im not using corners

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 11, 2016
  4. Offline


    No need. Random is apart of the Java "API". If you need more help, this is the exact math for what you want

    X value = random.nextInt(max distance*2)-maxDistance;

    This will give you the range (negitive max Distance) to (Max Distance). Then add this X value to that of your center point (e.g. If maxDistance = 10, then 500 + X will return between 490 and 510)
  5. Offline


    Try with this method
        public static Location generateRandomLocationAround(Location l, int maxdistance) {
            Random rnd = new Random();
            int x = rnd.nextInt(maxdistance);
            int z = rnd.nextInt(maxdistance);
            boolean negativex = rnd.nextBoolean();
            boolean negativez = rnd.nextBoolean();
            if (negativex) {
                x = x*-1;
            if (negativez) {
                x = x*-1;
            return l;
    It returns a random location around the location that you specified, with a max radious. (Untested, may not work)
  6. Offline


    This will not work because you are generating coordinates out from your max distance instead of adding the random distance to the current coordinate. Also a negative coordinate is the opposite of a positive coordinate meaning that it can be thousands of blocks away depending on how high your current coordinates are. Also you are spoonfeeding.

    @webbhead, I'm not going to spoonfeed but I'll give you a couple of steps to follow.

    1. Find your lowest corner coordinate and store it
    2. Use a max distance with Random (
    3. Add your generated distances to your current coordinates
    4. Teleport the player

    Random example:
    Random rand = new Random();
    int  distance = rand.nextInt(50);
    //This will generate a distance from 0 to 50, and adding
    //this to the current x/y coordinate will make your
    //player move up to 50 blocks.
Thread Status:
Not open for further replies.

Share This Page