[英]Generate random number in java
我正在使用一些简单的代码,应该从配置中获取数据
我的配置看起来像:
name: test
locationA: -457.0,5.0,-186.0
locationB: -454.0,5.0,-186.0
prisonfile: ./plugins/JB/test.prison
prisons:
- -454.0,4.0,-176.0
- -460.0,4.0,-176.0
- -457.0,5.0,-186.0
police:
- -460.0,5.0,-186.0
- -454.0,5.0,-186.0
- -457.0,5.0,-176.0
open: true
我的代码如下:
public void enter(Player player, String lines, String lines2)
{
World world = player.getWorld();
HashMap<String, Object> prison = plugin.prisons.getPrison(world.getName(), false);
File configFile = new File(prison.get("config").toString());
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
String listName = "police";
List<String> list = config.getStringList(listName);
Integer ListSize = list.size();
Random r = new Random();
int i1=r.nextInt(ListSize-1);
String[] parts = list.get(i1).split(",");
player.teleport(new Location(world, Float.parseFloat(parts[0]), Float.parseFloat(parts[1]), Float.parseFloat(parts[2])));
代码可以正确运行并在随机位置上传送我,但是它始终只在前2个位置上传送,而从不在第3个位置上传送我,我尝试打印出在配置及其3中找到ListSize的协调次数,所以我完全不了解。
ps我需要生成0到MaxNumber之间的随机数
您需要良好的随机性和良好的种子...所以您可以使用以下一种
java.util.Random random = null; // Declare the random Instance.
random = new java.util.Random(
System.currentTimeMillis());
// or
random = new java.util.Random(System.nanoTime());
// or
random = new java.util.Random(System.nanoTime()
^ System.currentTimeMillis());
// or
random = new java.security.SecureRandom(); // Because it makes "security" claims! :)
random.nextInt(MaxNumber + 1); // for the range 0-MaxNumber (inclusive).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.