0

I'm making a plugin for a Minecraft server that will let a player select two locations (x1, z1 (first location): x2, z2 (second location)) and allow them to set this area in between the two points (rectangular/square) to randomaly teleport them anywhere in the given locations.

For the sake of simplicity, I will leave most of the code out and just give you the segment I'm having trouble with. Below, this code will (on the event that a player joins the server) teleport them inside that area.

I've setup some dummy data inside nextInt() so you can understand the math.

Location 1 (x1, z1): -424, 2888
Location 2 (x2, z2): 4248, 3016

Above are the locations in the proram segment below. (Think of "z" as "y" on a graph).

@EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
        Player player = event.getPlayer();

        int x = 0, y = 0, z = 0;
        Random randLocation = new Random();

        player.sendMessage(ChatColor.RED + "TELEPORTING TO WASTELAND..");

        x = randLocation.nextInt(((2888 - 424) + 1) + 424);
        z = randLocation.nextInt(((4248 - 3016) + 1) + 3016);
        Location location = player.getLocation();
            location.setX(x);
            location.setZ(z);
            location.setY(player.getWorld().getHighestBlockAt(x, z).getY());
        player.teleport(location);
}

The problem is, sometimes one (or maybe both) of the locations have a negative value. I have tried and tried different methods of coming up with these numbers but I am stumped.

QUESTION: Is there anyway to make Java select a random number between 2 givens?

Example:

randomLocation.nextInt(x1, x2);
randomLocation.nextInt(z1, z2);
Logan Butler
  • 159
  • 1
  • 2
  • 9
  • 1
    http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range – Eric Guan Mar 09 '16 at 05:18
  • Yeah, I've looked at those and they didn't help. As you can see, I adopted one of the answers methods and couldn't get it working. – Logan Butler Mar 09 '16 at 05:21
  • [`nextInt`](http://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-) always returns a positive number. You need to more clearly explain how your question is different from that one and give us more information about the problem. (The code you've shown us just won't result in negative numbers because `nextInt` doesn't return them.) Please see http://stackoverflow.com/help/mcve. – Radiodef Mar 09 '16 at 05:27

2 Answers2

0

You have a mistake in the code determining the random coordinates:

x = randLocation.nextInt(((2888 - 424) + 1) + 424);
z = randLocation.nextInt(((4248 - 3016) + 1) + 3016);  

You are using x1 and z1 to determine the new x location, when you should be using x1 and x2:

randX = randLocation.nextInt(Math.abs(x2-x1) + 1) + Math.min(x1,x2);
randZ = randLocation.nextInt(Math.abs(z2-z1) + 1) + Math.min(z1,z2);
Maljam
  • 6,244
  • 3
  • 17
  • 30
  • Well that fixed it. Can you explain what you did beside change the way I used my variables? – Logan Butler Mar 09 '16 at 05:38
  • Only one problem: Why is the z location so closely together? – Logan Butler Mar 09 '16 at 05:43
  • The idea is that the new value will have a minimum value `Math.min(x1,x2)` and can be at most `Math.abs(x2-x1) + 1` away. So you simply add the minimum value to a random number between 0 and the max range. – Maljam Mar 09 '16 at 05:45
  • What if both locations are well past 0? – Logan Butler Mar 09 '16 at 05:47
  • @LoganButler Random doesn't mean always evenly distributed data! you should do a few hundred calls and plot them to see if there's a bias, not 6! And yes, it's fine if the locations are well past 0. – Maljam Mar 09 '16 at 05:48
0
x = randLocation.nextInt((2888 - 424) + 1) + 424;
z = randLocation.nextInt((4248 - 3016) + 1) + 3016;

One more thing: it should be like this: assuming x2>x1 and z2>z1

x = randLocation.nextInt((x2 - x1) + 1) + x1;
z = randLocation.nextInt((z2 - z1) + 1) + z1;