Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用随机数填充数组并冻结/保护数组中的数字_Java_Arrays_Random_Fill - Fatal编程技术网

Java 如何用随机数填充数组并冻结/保护数组中的数字

Java 如何用随机数填充数组并冻结/保护数组中的数字,java,arrays,random,fill,Java,Arrays,Random,Fill,如何用随机数填充数组,然后将这些数字保存在数组中,然后不再随机 这是我的密码: public static void main (String[] args){ int[] Array = new int [10] ; for (int i = 0; i < Array.length; i++){ Array[i] = (int) (Math.random() * 100); System.out.println(Array[i]); }

如何用随机数填充数组,然后将这些数字保存在数组中,然后不再随机

这是我的密码:

public static void main (String[] args){
    int[] Array = new int [10] ;
    for (int i = 0; i < Array.length; i++){
      Array[i] = (int) (Math.random() * 100);
      System.out.println(Array[i]);
    }

}

如果希望每次运行程序时随机数序列相同,请使用new Randomlong创建一个具有常数种子的随机数生成器,并使用该生成器生成数字

无法创建其元素无法更改的Java数组。Java语言不支持这一点

import java.util.Random;

public class Solution {
    public static void main(String args[]) {
        // give any random number as seed depend on which your random numbers are
        // generated
        // and as long as seed is same the random numbers generated are same
        long seed = 792839098;
        Random random = new Random(seed);
        int Array[] = new int[10];
        for (int i = 0; i < Array.length; i++) {
            Array[i] = random.nextInt();
        }
        System.out.println("your same random numbers are ");
        for (int i = 0; i < Array.length; i++) {
            System.out.println(Array[i]);
        }
    }
}

这个答案符合你的问题。但如果你想一次又一次地创造同样的随机数,同样的随机数句子会自相矛盾。变得矛盾。它们不再是随机的地方。

你说的冻结/保存数字是什么意思?每次我编译代码时,数组中的数字都会改变,我不想这样。是的……但是如果你冻结数字,那么它们就不会是随机的。即使是现在,它们也不是随机的,因为正在使用种子。请阅读并相应地增强您的问题。不要在评论中添加更多信息,而是更新您的问题。