我目前正在为学校编写一个程序来测试不同排序算法的效率。尝试创建随机数数组时,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())
来播种但错误仍然每次都出现。
这是错误的屏幕截图:
最佳答案
nextInt()
有以下检查
if (bound <= 0)
throw new IllegalArgumentException(BadBound);
作为您的第一个参数传递给 nextInt()
为零,你得到 p>
java.lang.IllegalArgumentException: bound must be positive
关于Java 对象 Random 总是返回错误 : "Random.nextInt(int) line: not available",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28658398/