简体   繁体   English

这两种随机方法有什么区别?

[英]What is the difference between these two random methods?

This is a rock, paper, scissors game. 这是一个摇滚,纸张,剪刀游戏。 My question is, if i moved weapons.length in private Random r = new Random(weapons.length); 我的问题是,如果我私下移动武器。长度随机r =新随机(武器。长度); it would give me an error. 它会给我一个错误。 While if i moved weapons.length inside the method it would run successfully. 如果我在方法中移动了weapon.length,它将成功运行。 What is the difference? 有什么区别?

 public class Game {

private String[] weapons = {"rock", "paper", "scissor"};
private Random r = new Random(weapons.length);


public void thePick() {

    System.out.println(weapons[r.nextInt()]);

 }

}

vs VS

public class Game {

private String[] weapons = {"rock", "paper", "scissor"};
private Random r = new Random();


public void thePick() {

    System.out.println(weapons[r.nextInt(weapons.length)]);

 }
}

r.nextInt() would give you any random integer, regardless of the array being there. 无论数组在哪里, r.nextInt()都会给你任意随机整数。 When you call private Random r = new Random(weapons.length); 当你打电话给private Random r = new Random(weapons.length); you are actually seeding the RNG with 3, and not setting an upper limit of 3. 你实际上是在给RNG播种3,而没有设置3的上限。

It could give you 42, 2,000,000, -10, etc, etc. The array doesn't have an element in position 42, and you'd get an error as the seed of 3 will always yield a random value out of range. 它可以给你42,2,000,000,-10等等。数组在位置42没有元素,你会得到一个错误,因为3的种子将总是产生超出范围的随机值。 If it was not seeded (ie Random r=new Random() ) then there is a slim chance of getting a valid value, but that chance is extremely small. 如果它没有播种(即Random r=new Random() )那么获得有效值的可能性很小,但这个机会非常小。

For the second, you are picking a random int with a bound , namely from 0 to the length of the array(not including the upper bound). 对于第二个,您将选择带有边界的随机int ,即从0到数组的长度(不包括上限)。

So for your: 所以对你来说:

{"rock", "paper", "scissor"};

rock is element 0, paper is element 1, and scissors is element 2. The random.nextInt(weapons.length) call always returns an int from 0 to 2(as 3, the length, is NOT included). rock是元素0,paper是元素1,剪刀是元素2. random.nextInt(weapons.length)调用总是返回0到2的int(3,长度,不包括在内)。

The first one will throw ArrayIndexOutOfBoundsException every time. 第一个将每次抛出ArrayIndexOutOfBoundsException。 In fact it will always throw the exception 实际上它总会抛出异常

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1155099828
    at Game.thePick(Game.java:11)

Thank you @hexafraction for spotting that. 感谢@hexafraction的发现。

The second method won't print an exception, instead it will just pick a random choice. 第二种方法不会打印异常,而是选择随机选择。

The second delivers a value from 0 - 2 第二个提供0到2的值
The first random delivers in whole int range, so it practically ever will throw an ArrayOutOfBoundsException. 第一个随机传递整个int范围,所以它几乎会抛出一个ArrayOutOfBoundsException。
But why you dont try out? 但为什么你不尝试?

You should consider reading the respective Javadocs: Here is the constructor you are invoke in your first attempt: http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#Random%28long%29 You are actually providing a seed not getting a random number in a specified range. 您应该考虑阅读相应的Javadoc:这是您在第一次尝试时调用的构造函数: http//docs.oracle.com/javase/7/docs/api/java/util/Random.html#Random%28long% 29您实际上提供的种子没有获得指定范围内的随机数。 This means that calls to .nextInt() will return the same on every run of your application. 这意味着对.nextInt()的调用将在每次运行应用程序时返回相同的内容。

In your second attempt you are using the default contrusctor (initializes with the current time as seed) and calling a method that does the right thing: http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt%28int%29 在您的第二次尝试中,您使用默认的控制器(使用当前时间初始化为种子)并调用一个正确的方法: http//docs.oracle.com/javase/7/docs/api/java/util /Random.html#nextInt%28int%29

Documentation is a wonderful thing. 文档是一件很棒的事情。 Use it wisely. 明智地使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM