Reputation: 537
I have created a method which takes generate random numbers with the condition that the next random number doesn't match with the previous one inside the array here's the code
// some code
int k=0;
//some code....
randomgenerator(k); // method call
public void randomgenerator(int j)
{
for(j=0; j<=99; j++){
if(j >= 1){
if (randomset.get(j) == randomset.get(j-1)){
randomset.add(0 + ( j , int)(Math.random() * ((99 - 0) + 1)));
}
else{
randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1)));
}
}
}
}
The error I get is java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
Upvotes: 2
Views: 2653
Reputation: 486
What is with you're function? You receive a parameter named j and then you reassigned it?
randomset.get(j) == randomset.get(j-1) <- at this line you have a java.lang.IndexOutOfBoundsException because you call for the value from position 1 but in you're list you have only a value on position 0; so an error is thrown
and, what is this ? ((99 - 0) + 1)) ,you could white 100, it is easy, and more readable
by the way, at this line you have an error, randomset.add(0 + ( j , (int)(Math.random() * ((99 - 0) + 1)));
You should write a cleaner code.
I've prepared a solution for you: a function which generates a List with random numbers and respects you condition: two consecutive numbers are not the same.
You must call this method generateRandomList with the number of elements you want to generate.
public static final Integer MAX_RANDOM_NUMBER = 100;
public static List<Integer> generateRandomList(int randomNumbers) {
return generateRandomList(randomNumbers, -1);
}
private static List<Integer> generateRandomList(final int randomNumbers, final int previousNumber) {
if (randomNumbers == 1) {
return new ArrayList<Integer>() {
{
add(getNextNumber(previousNumber));
}
};
} else {
return new ArrayList<Integer>() {
{
int value = getNextNumber(previousNumber);
add(value);
addAll(generateRandomList(randomNumbers - 1, value));
}
};
}
}
private static int getNextNumber(int previousNumber) {
boolean generateNewValue = true;
int currentValue = 0;
while (generateNewValue) {
currentValue = (int) (Math.random() * MAX_RANDOM_NUMBER);
generateNewValue = currentValue == previousNumber;
}
return currentValue;
}
Upvotes: 0
Reputation: 16054
You can't reference an element of ArrayList
whose index is not in bounds [0, size() - 1]
. Creating ArrayList
via ArrayList()
creates a list of size 0
. To add elements to this array you must call one of the methods that adds an element, e.g. add()
. Your first call is to get()
, but the list has size 0
, so even get(0)
will cause an IndexOutOfBoundsException
.
What to do depends on the expected contents of the list. In your case, I would recommend writing a helper function that generates a random number in range excluding specified number. You could use that function in a simple loop to generate the whole list, passing previous element to mentioned helper function.
Example:
public static int randomInRange(int a, int b) {
return (int)(Math.random() * (b - a + 1));
}
public static int randomInRangeExcluding(int a, int b, int excluding) {
int result = (int)(Math.random() * (b - a));
if (result == excluding) {
result++;
}
return result;
}
public static List<Integer> generateRandomList(int size) {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int j = 0; j <= size; j++) {
if (j > 0) {
result.add(randomInRangeExcluding(0, size - 1, result.get(j - 1)));
} else {
result.add(randomInRange(0, size - 1));
}
}
return result;
}
and get the value using:
generateRandomList(100);
Calling this results in a list of random integers having no two consecutive elements equal:
[27, 34, 53, 92, 56, 93, 21, 22, 45, 95, 48, 25, 18, 26, 54, 1, 82, 26, 5, 62, 84, 23, 8, 84, 25, 0, 36, 37, 54, 95, 4, 26, 65, 53, 81, 16, 47, 56, 73, 46, 60, 50, 37, 89, 61, 84, 23, 79, 47, 87, 68, 49, 15, 17, 55, 71, 17, 55, 71, 51, 67, 33, 80, 47, 81, 24, 10, 41, 76, 60, 12, 17, 96, 43, 57, 55, 41, 56, 21, 85, 98, 40, 9, 39, 53, 28, 93, 70, 89, 80, 40, 41, 30, 81, 33, 53, 73, 28, 38, 87, 29]
Upvotes: 0
Reputation: 2419
Because initially randomset is empty therefore its size is 0 and returns exception at index 1. The best way to add randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1)));
if j < 1
(not >=1).
Correct code:
public void randomgenerator(int j)
{
for(j=0; j<=99; j++){
if(j >= 1){
if (randomset.get(j) == randomset.get(j-1)){
randomset.add(0 + ( j , int)(Math.random() * ((99 - 0) + 1)));
}
else{
randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1)));
}
}
else {
randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1)));
}
}
}
Upvotes: 1
Reputation: 461
Don't use the same variable as your input parameter and your loop variable.
public void randomgenerator(int length) {
for (int j = 0; j < length; j ++) ...
I'm not sure I follow the rest of the code, but that's a start.
Upvotes: 0