[英]Printing random even value between two values using inheritance
I am trying to print a list of random even numbers (5 times) using a bounds.我正在尝试使用边界打印随机偶数列表(5 次)。 Example being from 0 to 30 (including both those numbers).示例是从 0 到 30(包括这两个数字)。 This is what I have so far (this is in its own class):这是我到目前为止所拥有的(这是在它自己的类中):
public int nextEven(int h){
int n = rand.nextEven(h) % 2;
return n;
}
This is where it would print from my main method:这是它从我的主要方法打印的地方:
System.out.println("Random Even:");
for (int i = 0; i < 5; i++){
System.out.println(rand.nextEven(30));
}
When I run the program it gives me an error and I am not quite sure how to solve this.当我运行程序时,它给了我一个错误,我不太确定如何解决这个问题。 This is an example of the desired output of even numbers from 0 to 30:这是从 0 到 30 的偶数所需输出的示例:
4 26 12 10 20 4 26 12 10 20
It isn't clear why taking the remainder of 2
would yield an even number.不清楚为什么取2
的余数会产生偶数。 Instead, generate a number in the range 0
to h / 2
and then multiply the result of that by 2
.相反,生成0
到h / 2
范围内的数字,然后将其结果乘以2
。 Like,喜欢,
public int nextEven(int h){
int n = ThreadLocalRandom.current().nextInt(1 + (h / 2)); // 0 to (h / 2) inclusive
return n * 2; // n * 2 is even (or zero).
}
What exactly is rand?兰特究竟是什么? Is it the Random class or an instance of your own class?它是 Random 类还是您自己的类的实例? Since you want to do something with inheritance I guess you want to overwrite a method, but if rand is an instance of the java Random class this won't work.因为你想用继承做一些事情,我猜你想覆盖一个方法,但如果 rand 是 java Random 类的一个实例,这将不起作用。
The error probably comes from recursively calling nextEven method forever.错误可能来自永远递归调用 nextEven 方法。
If you could clarify what exactly you want to do?如果你能澄清一下你到底想做什么?
The mod operator %
will give you the remainder of the first value divided by the second. mod 运算符%
将为您提供第一个值除以第二个值的余数。
value % 2
... will return 0 if value
is even, or 1 if value
is odd. ...如果value
偶数,则返回 0,如果value
奇数,则返回 1。
Since rand
is a reference to an instance of the class containing your code, you have an infinite recursion.由于rand
是对包含您的代码的类的实例的引用,因此您具有无限递归。 What you really need is something like:你真正需要的是这样的:
public int nextEven(int h){
int evenRandomValue;
do {
evenRandomValue = (int)(Math.random() * (h + 1));
} while(evenRandomValue % 2 == 1);
return evenRandomValue;
}
I see at least two solutions.我看到至少两种解决方案。
The first one supposes that random + 1 = random
.第一个假设random + 1 = random
。 I mean, that if you add or subtract a random number you still get a valid random number.我的意思是,如果您添加或减去一个随机数,您仍然会得到一个有效的随机数。 That's why you can use Random
class to generate a value in the desired period and then add or subtract one it the number is odd.这就是为什么您可以使用Random
类在所需时间段内生成一个值,然后在数字为奇数时加上或减去一个值。
The second approach is just to generate an array of even values for the desired period.第二种方法只是生成所需时间段的偶数值数组。 Then take a random value from this array.然后从这个数组中取一个随机值。
Here is a quite explicit way to achieve this using streams:这是使用流实现此目的的一种非常明确的方法:
List<Integer> myRandomInts = Random.ints(lower, upper + 1)
.filter(i -> i % 2 == 0)
.limit(5).boxed()
.collect(Collectors.toList());
This can be read as 'generate an infinite stream of random numbers between given bounds, filter out odds, take the first 5, turn into Integer
objects and then collect into a list.这可以理解为“在给定的边界之间生成无限的随机数流,过滤掉赔率,取前 5 个,变成Integer
对象,然后收集到一个列表中。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.