Java object Random always returns error: "Random.nextInt(int) line: not available"

Java object Random always returns error: "Random.nextInt(int) line: not available"

我目前正在为学校编写一个程序来测试不同排序算法的效率。尝试创建随机数数组时,Random 对象总是出错。

arrays 是类型 ArrayList<ArrayList<Integer>> 并且是一组我测试的数组

numTrials 是类型 int 并且是每个数组大小的试验次数(我正在以不同的 10 次幂测试不同的数组大小)

这是我现在填充数组的代码:

Random randGen = new Random();

for(int i = 0; i < arrays.size(); i++)
{
    for(int j = 0; j < Math.pow(10.0, i / numTrials); j++)
    {
        arrays.get(i).set(j, randGen.nextInt(i));
    }
}

我也尝试通过调用 randGen.setSeed(System.currentTimeMillis()) 进行播种,但每次仍然出现错误。

这是错误的屏幕截图:

你一定得到了类似 random number's bound must be positive 的东西,因为你只能为正数生成随机数。

nextInt() 有以下检查

if (bound <= 0)
            throw new IllegalArgumentException(BadBound);

由于您传递给 nextInt() 的第一个参数为零,因此您得到

java.lang.IllegalArgumentException: bound must be positive