The_Architect
The_Architect

Reputation: 1

What's wrong with my random search?

I get the error: "ArrayIndexOutOfBounds Exception3" I don't think that is the only error, but so far that's all I've been able to figure out.

import java.util.Random;
/*class containing random search algorithm*/
public class RandomSearch {
    public static int randomSearch(int queryValue, int[] list) {
    /*conducts a random search as specified by user*/
    /*trys 10,000,000 random combinations searching
      for user value*/
    int length = list.length;
    for(int i=0; i < 10000000; i++) {
        /*generates a random number from 0 to length*/
        int randomNum = (int)Math.floor(Math.random()*(length+1));
        StdOut.print(randomNum);
        if((int)queryValue == (int)list[randomNum]) {
         return randomNum;
        }
    }
    /*returns -2 if user value not found*/
    return -2;
    }
}

Upvotes: 0

Views: 86

Answers (6)

Bohemian
Bohemian

Reputation: 425238

The standard approach is:

 int randomNum = (int)(Math.random()*length);

You don't need Math.floor() because casting to int has the same effect.

Also just multiply by length (not length+1, to give you numbers from 0 to length-1.

Or simply use the JDK's API:

int randomNum = new Random().nextInt(length);

Upvotes: 0

Anton Boritskiy
Anton Boritskiy

Reputation: 1569

Let's say your list.length length is 100, then if Math.random() return 0.999, this statement

(int)Math.floor(Math.random()*(length+1));

will return 100. While 100 is out of your list bounds, since valid indexes for array with length 100 are from 0 to 99.

Answer was updated according to http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#random()

Upvotes: 0

bobiczeq
bobiczeq

Reputation: 93

Try to use java.util.Random method:
Random rand = new Random();
int randomNum = rand.nextInt(lenght+1); It gives u number from 1 to lenght.

Upvotes: 0

user2535749
user2535749

Reputation: 35

Array indexes start at 0, so when you call length() on an array the last index will actually be length - 1. If you try any index after list[length - 1] you will get the Array out of bounds error.

Upvotes: 0

Bhanu Kaushik
Bhanu Kaushik

Reputation: 864

You need something like this to generate the index this gives the index between 0 and length The index you are generating is beyond the size of array thats why you are getting index out of bound exception.

/*Edit - Changed val to length to match the question.*/
public int randInt(int length) {

  // Usually this can be a field rather than a method variable
  Random rand = new Random();
  int randomNum = rand.nextInt(length);
  return randomNum;
}

Upvotes: 2

Christian Tapia
Christian Tapia

Reputation: 34166

The issue is that randomNum can get a value larger than the maximum index that can be used to access the array list.

To solve this, change

int randomNum = (int) (Math.random() * (length + 1));

to

int randomNum = (int) (Math.random() * (length));

Upvotes: 0

Related Questions