用java生成随机数
我使用一些简单的代码,应该从配置中获取数据 我的配置看起来像:用java生成随机数,java,list,random,Java,List,Random,我使用一些简单的代码,应该从配置中获取数据 我的配置看起来像: 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,-1
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])));
我的代码如下所示:
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])));
public void enter(播放器、字符串行、字符串行2)
{
World=player.getWorld();
HashMap监狱=plugin.inspires.get监牢(world.getName(),false);
File configFile=新文件(security.get(“config”).toString());
FileConfiguration config=YamlConfiguration.loadConfiguration(configFile);
字符串listName=“police”;
List List=config.getStringList(listName);
整数ListSize=list.size();
随机r=新随机();
int i1=r.nextInt(列表大小-1);
String[]parts=list.get(i1).split(“,”);
player.teleport(新位置(world,Float.parseFloat(部分[0]),Float.parseFloat(部分[1]),Float.parseFloat(部分[2]);
代码正确地工作并在随机位置传送我,但它总是只在前2个位置上传送,而从不在第3个位置上传送,我试图打印出在配置中找到的ListSize及其3的协调数量,所以我完全不理解
p、 s.我需要在0和MaxNumber of positions之间生成随机数问题在于这一行中的
nextInt
方法的参数:
int i1=r.nextInt(ListSize-1);
返回的随机数范围为0
(含)n-1
,n
为参数。引用自:
返回从该随机数生成器的序列中提取的0(包括)和指定值(排除)之间的伪随机、均匀分布的int值
(强调矿山)
这里不需要从列表大小中减去1
。请尝试
int i1 = r.nextInt(ListSize);
问题在于此行中
nextInt
方法的参数:
int i1=r.nextInt(ListSize-1);
返回的随机数范围为0
(含)n-1
,n
为参数。引用自:
返回从该随机数生成器的序列中提取的0(包括)和指定值(排除)之间的伪随机、均匀分布的int值
(强调矿山)
这里不需要从列表大小中减去1
。请尝试
int i1 = r.nextInt(ListSize);
你需要一个好的随机数和一个好的种子…这样你就可以使用其中的一个
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).
你需要一个好的随机数和一个好的种子…这样你就可以使用其中的一个
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).