# 一种简单的思路
```java
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
int num = (int) (Math.random() * arr.length);
arr[i] = num;
System.out.println(num);
}
2
4
4
1
1
可以看出要生成0-5之间的数(不包含5),不就是一个数组长度减一吗??岂不就是随机产生数组下标
这是package org.apache.commons.lang3;
public static int nextInt(final int startInclusive, final int endExclusive) {
Validate.isTrue(endExclusive >= startInclusive,
"Start value must be smaller or equal to end value.");
Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative.");
if (startInclusive == endExclusive) {
return startInclusive;
}
return startInclusive + RANDOM.nextInt(endExclusive - startInclusive);
}
return startInclusive + RANDOM.nextInt(endExclusive - startInclusive);直接套用大佬的,表示生成两个数之间的随机整数