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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:05:58  来源:igfitidea点击:

How to fill an array with random numbers from 0 to 99 using the class Math?

javaarraysrandom

提问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?

为什么?

  1. Using Math.random()can return 1, this means Math.random()*100can return 100, but OP asked for maximum 99! Using nextInt(100)is exclusive 100, it can only return values from 0 to 99.
  2. Math.random()can not return -0.000001that would be round to 0and1.0000001can not be returned that should round to 1. So you have less chance to get 0or 99than all the numbers between. This way it is not realy random, to guess "its not 0or 99" is more true than "its not 1or 98".
  3. Also it do not make a detour via casting and mathematic operations you don't realy need, hey you dont need to strictfpon amd-cpus or old intel-cpus.
  1. 使用Math.random()can return 1,这意味着Math.random()*100可以返回 100,但是 OP 要求最大 99!使用nextInt(100)是独占 100,它只能返回 0 到 99 之间的值。
  2. Math.random()不能返回-0.000001将舍入到0并且1.0000001不能返回到应舍入到1。因此,您获得099之间的所有数字的机会都较小。这样它就不是真正随机的,猜测“它不是099”比“它不是198”更真实。
  3. 此外,它不会通过您并不真正需要的转换和数学运算绕道而行,嘿,您不需要strictfp在 amd-cpus 或旧的 intel-cpus 上。

回答by user1932890

This is not actually using the java.lang.Mathclass, 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;
}