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

I am currently writing a program for school to test the efficiency of different sorting algorithms. When trying to create arrays of random numbers, the Random object always gives an error.

arrays is type ArrayList<ArrayList<Integer>> and is a set of the arrays I test

numTrials is type int and is the number of trials per size of array (I am testing different array sizes at different powers of 10)

This is my code to fill the arrays right now:

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));
    }
}

I tried seeding as well by calling randGen.setSeed(System.currentTimeMillis()) but the error still showed up every time.

Here is a screengrab of the error: Error screengrab

Answer

nextInt() has following check

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

As your first param passed to the nextInt() is zero, you are getting

java.lang.IllegalArgumentException: bound must be positive

Leave a Reply

Your email address will not be published. Required fields are marked *