我们首先创建一个数组-
double[] arr = new double[5];
现在,创建Random类对象-
Random randNum = new Random();
现在,用随机数填充上面的数组-
for (int i = 0; i < 5; i++) { arr[i] = randNum.nextInt(); }
import java.util.Arrays; import java.util.Random; public class Demo { public static void main(String args[]) { double[] arr = new double[5]; Random randNum = new Random(); for (int i = 0; i < 5; i++) { arr[i] = randNum.nextInt(); } System.out.println("Random numbers = "+Arrays.toString(arr)); } }
输出结果
Random numbers = [-6.59888981E8, 1.141160731E9, -9.931249E8, 1.335266582E9, 8.27918412E8]