yourselvs
yourselvs

Reputation: 451

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

Upvotes: 4

Views: 929

Answers (2)

vtor
vtor

Reputation: 9329

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

Upvotes: 4

SMA
SMA

Reputation: 37093

You must be getting something like random number's bound must be positive since you can generate random numbers only for positive numbers.

Upvotes: 1

Related Questions