Java 如何使用类 Math 用从 0 到 99 的随机数填充数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25768435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fill an array with random numbers from 0 to 99 using the class Math?
提问by Yakov Stoykov
I wrote the code, but there is no conversion from double to int.
我写了代码,但是没有从double到int的转换。
public class Array {
public static void main(String[] args) {
int i;
int[] ar1 = new int[100];
for(int i = 0; i < ar1.length; i++) {
ar1[i] = int(Math.random() * 100);
System.out.print(ar1[i] + " ");
}
}
}
How can it be corrected?
如何纠正?
采纳答案by Thusitha Thilina Dayaratne
It should be like
它应该像
ar1[i] = (int)(Math.random() * 100);
When you cast, cast type should be in brackets e.g. (cast type)value
转换时,转换类型应在括号中,例如 (cast type)value
回答by talex
ar1[i] = (int)(Math.random() * 100);
Conversion in Java looks like cast in C.
Java 中的转换看起来像 C 中的强制转换。
回答by Grim
Try this:
尝试这个:
package studing;
public class Array {
public static void main(String[] args) {
Random r = new Random();
int[] ar1 = new int[100];
for(int i = 0; i < ar1.length; i++) {
ar1[i] = r.nextInt(100);
System.out.print(ar1[i] + " ");
}
}
}
Why?
为什么?
- Using
Math.random()
can return 1, this meansMath.random()*100
can return 100, but OP asked for maximum 99! UsingnextInt(100)
is exclusive 100, it can only return values from 0 to 99. Math.random()
can not return-0.000001
that would be round to0
and1.0000001
can not be returned that should round to1
. So you have less chance to get0
or99
than all the numbers between. This way it is not realy random, to guess "its not0
or99
" is more true than "its not1
or98
".- Also it do not make a detour via casting and mathematic operations you don't realy need, hey you dont need to
strictfp
on amd-cpus or old intel-cpus.
- 使用
Math.random()
can return 1,这意味着Math.random()*100
可以返回 100,但是 OP 要求最大 99!使用nextInt(100)
是独占 100,它只能返回 0 到 99 之间的值。 Math.random()
不能返回-0.000001
将舍入到0
并且1.0000001
不能返回到应舍入到1
。因此,您获得0
或99
之间的所有数字的机会都较小。这样它就不是真正随机的,猜测“它不是0
或99
”比“它不是1
或98
”更真实。- 此外,它不会通过您并不真正需要的转换和数学运算绕道而行,嘿,您不需要
strictfp
在 amd-cpus 或旧的 intel-cpus 上。
回答by user1932890
This is not actually using the java.lang.Math
class, but in Java 8 a random array can also be created in this fashion:
这实际上并未使用java.lang.Math
该类,但在 Java 8 中也可以以这种方式创建随机数组:
int[] random = new Random().ints(100, 0, 100).toArray();
int[] random = new Random().ints(100, 0, 100).toArray();
回答by user3743369
My solution uses Random class instead of Math.random. Here it is.
我的解决方案使用 Random 类而不是 Math.random。这里是。
private static int[] generateArray(int min, int max) { // generate a random size array with random numbers
Random rd = new Random(); // random class will be used
int randomSize = min + rd.nextInt(max); // first decide the array size (randomly, of course)
System.out.println("Random array size: " + randomSize);
int[] array = new int[randomSize]; // create an array of that size
for (int i = 0; i < randomSize; i++) { // iterate over the created array
array[i] = min + rd.nextInt(max); // fill the cells randomly
}
return array;
}