que1326
que1326

Reputation: 2325

How to random without errors inside a while loop?

I have a public double method: bisectie with 2 double parameters, When I random in a range using those parameters , I ve got some errors I can t deal with them.

"Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Unknown Source)
    at BisectieOptimizata.bisectie(BisectieOptimizata.java:23)
    at BisectieOptimizata.main(BisectieOptimizata.java:39) "

Below is the source code:

import java.util.Random;

public class BisectieOptimizata {

    public  double c;
    public double eps = 0.01;

    //test function
    public double functie(double x){
        return x*x - 2*x + 1;
    }
    //derivata
    public double derivata(double x){
        return (functie(x+eps)-functie(x))/eps;
    }
    //bisection algh.
    public double bisectie(double a, double b){

        Random rand = new Random();
        double err = 0.001;

        while(Math.abs(b-a)>err){

            c = rand.nextInt((int)((b - a) + 1)) + a + rand.nextDouble(); // random a number from a range, ex: [-10;11), I random 4.542 

            if(derivata(c)<0){
                if(derivata(c)<a) a = c-a;
                else a = c;
            }else {
                if(derivata(c)>b) b = c-b;
                else b = c;

            }
        }
        return (a+b)/2;
    }
    public static void main(String[] args){

        BisectieOptimizata BO = new BisectieOptimizata();
        System.out.println(BO.bisectie(-10, 10)); // a = -10, b = 10
    }
}

Upvotes: 0

Views: 152

Answers (2)

chettyharish
chettyharish

Reputation: 1734

I observed that you are comparing (Math.abs(b - a) > err) so a negative number can also be greater than 0.001 as it is Math.abs.

This will cause your value inside rand.nextInt() to go negative.

The values of a and b change as follows in the program

10.0
-10.0

10.0
-2.5993084021357675

0.1896164396715161
-2.5993084021357675

0.1896164396715161    
1.5161487751844813  ---> error

Here (a-b)+1 = -0.32653233551. Now the Math.abs(b - a) value is 1.32653233551 which obviously qualifies the loop criterion.

But when the value (a-b)+1 is passed into rand.nextInt(), it will generate an exception as the number is negative.

I am not sure what you are doing with the algorithm, but removing Math.abs does solve the problem.

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38511

The exception provides meaningful information:

"Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Unknown Source)
    at BisectieOptimizata.bisectie(BisectieOptimizata.java:23)
    at BisectieOptimizata.main(BisectieOptimizata.java:39) "

So, have you ensured that the argument to nextInt is positive wherever you use it in your code? It appears not.

rand.nextInt((int)((b - a) + 1)) + a + rand.nextDouble();

Perhaps you forgot to perform Math.abs here? Without more information, it's hard to know. Debugging this problem in an IDE would be easy. You can set an 'Exception Breakpoint' in Intellij, and catch your program at the point in which this exception is thrown, and examine the call stack and variables available in memory.

Or just add this print statement before your call to nextInt:

        System.out.println((int)((b - a) + 1));

and you'll notice it only fails when the value of the expression is negative.

Upvotes: 0

Related Questions