生成给定范围内的随机位置(可以是正数或负数)

时间:2015-08-03 09:58:48

标签: java random location bukkit radius

目前我有这段代码。

public static Location getRandomLocation(int min, int max, World w) {
    int x = CommonUtils.randomInt(min, max);
    int y = 255;
    int z = CommonUtils.randomInt(min, max);
    while (w.getBlockAt(x, y, z).getType() == Material.AIR) {
        y--;
    }
    return w.getBlockAt(x, y + 1, z).getLocation();
}

我想生成x和z坐标的随机值,也可以是负值。我遇到的问题是,我需要数字的绝对值仍然大于min值,即使实际数字不是。min

更简单地说,现在它只能在max-min之间生成随机位置。我如何还包括介于-maxwith sample_data as (select sysdate dt, 1 val from dual union all select sysdate - 31.53 dt, 10 val from dual union all select sysdate + 366 dt, 100 val from dual) select to_char(dt, 'fmMonth yyyy') my, sum(val) from sample_data group by to_char(dt, 'fmMonth yyyy') order by to_date(to_char(dt, 'fmMonth yyyy'), 'fmMonth yyyy'); MY SUM(VAL) -------------------------------- ---------- July 2015 10 August 2015 1 August 2016 100 之间的数字?

2 个答案:

答案 0 :(得分:0)

好的,现在我得到了你想要的东西。如果您只需要使其适用于负数,那么您已经拥有了基本可用的代码。一个简单的解决方法是在已生成的数字上添加一个随机符号。这样的事应该可以正常工作:

public static Location getRandomLocation(int min, int max, World world) {
    int x = CommonUtils.randomInt(-max, max);//Random x coord
    int z;
    if (Math.abs(x) < min) {
        z = CommonUtils.randomInt(min, max);//If x coord is less than the minimum choose a z coord that is within range
        if (new Random().nextBoolean()) {//Top or bottom of range
            z = -z;
        }
    } else {
        z = CommonUtils.randomInt(-max, max);//Else choose any z coord
    }
    int y = 255;
    while (world.getBlockAt(x, y, z).getType() == Material.AIR) {
        y--;
    }
    return world.getBlockAt(x, y + 1, z).getLocation();
}

答案 1 :(得分:0)

有一点要补充kmecpp所说的,因为我无法评论,不要遍历所有Y块以检查它们是否是空气。这比使用getHighestBlockYAt(x, z);要长。这将返回该位置的世界最高区块的Y位置。例如:

//Get your Random location with random x and y, and set the y to something like 5.
int newY = getServer().getWorld(worldName).getHighestBlockYAt(loc.getX(), loc.getZ());
Location newLoc = new Location(loc.getWorld(), loc.getX(), newY, loc.getZ());

关于带负数和正数的范围内的随机传送,这是我在服务器上使用的代码。 (注意,这会将它们传送到产卵中)

这会将玩家传送到一个12000块广场的随机位置,以0,0为中心。

Random rand = new Random();
int max = 6000;
int min = -6000;
int locX = rand.nextInt((max - min) + 1) + min;
int locZ = rand.nextInt((max - min) + 1) + min;
Location loc = new Location(getServer().getWorld("wild"), locX, 20, locZ);
loc.setY(getServer().getWorld("wild").getHighestBlockYAt(locX, locZ));
Block b = getServer().getWorld("world").getBlockAt((int) loc.getX(), (int) loc.getY() - 1, (int) loc.getZ());
while(b.getType() == Material.LAVA || b.getType() == Material.WATER){
    locX = rand.nextInt((max - min) + 1) + min;
    locZ = rand.nextInt((max - min) + 1) + min;
    loc = new Location(getServer().getWorld("wild"), locX, 20, locZ);
    loc.setY(getServer().getWorld("wild").getHighestBlockYAt(locX, locZ));
    b = getServer().getWorld("world").getBlockAt((int) loc.getX(), (int), loc.getY() - 1, (int) loc.getZ());
}
return loc;

如果你想让它们不传送到spawn,你需要再设置6个int,spawnXMin,spawnXMax,以及Y和Z的相应值。然后只需做一个简单的检查:

int spawnXMin = 100, spawnXMax = 200, spawnYMin = 0, spawnYMax = 255, spawnZMin = 150, spawnZMax = 250;
while(!(loc.getX() < spawnXMax && loc.getX() > spawnXMin && loc.getY() < spawnYMax && loc.getY() > spawnMin && loc.getZ() < spawnZMax && loc.getZ() > spawnZMin)) {
    //Player is not in spawn so redo above while code
}
//Found Location!
//Teleport Player