0

I have an ArrayList that contains numbers (1-50). Now I want to generate random numbers from this ArrayList (each random number must be an item from the ArrayList) and once that item is picked, it will be remove from the ArrayList.

This is what I have tried but the Random object keeps generating numbers that are not in the ArrayList

Random rnd = new Random();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
    
while (list2.size() < 50) {
    int index = list1.get(rnd.nextInt(list1.size()));
    list2.add(index);
    list1.remove(index);
}

The above code keeps generating random numbers that are not in the list1 object.

I need only numbers that are in list1 to be generated.

7
  • If so, please show your method list1. Commented Dec 2, 2021 at 14:43
  • I'm honestly stumped as to how this isn't working and I think I'd like to see more of your code. Because the rnd.nextInt() you have there should be returning you a number between 0 and 49. That being said, your list1.remove() may be removing the wrong number. In this case, if your random number was 29, you'd be adding a 30 to list2 (because that's what's at index 29 of list1) but removing the 31 from list1 (because that's what's at index 30, which is what index is set to now) Commented Dec 2, 2021 at 14:48
  • @Raphael Morgan: That's the point, the rnd.nextInt() generates any number between 0 and 49 but this is not what i want. The number it has to generate must be an element in list1 object Commented Dec 2, 2021 at 14:59
  • I know, but you aren't removing the object you queried. the rnd.nextInt gets you an index, and the object you want at that index (what you're setting to "index") is what's being added to list2 but NOT what's being added to list1. It's trying to remove the number at that index, not that number. Eg list1.remove(50) wouldn't work even if the #50 is in the list because the indices are 0-49 at most. Commented Dec 2, 2021 at 15:03
  • 1
    @Raphael Morgan: i now understand that the rnd.nextInt() deals with index of a value but not the actual value itself which means... if i'm sending list1.remove(10) , value 10 is not removed but a value at index 10. Thanks for the explanation Commented Dec 2, 2021 at 15:21

5 Answers 5

1

From what I understand, you are picking a random number from one list, removing it (note that there's a difference in remove(Integer o) and remove(int index)) , and inserting into another.

This is the exact same operation as shuffling the original list, making a copy, then clearing it

List<Integer> list1 = IntStream.range(1,51).collect(Collectors.toList());
Collections.shuffle(list1);
List<Integer> list2 = new ArrayList<>(list1);
list1.clear();
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

while (list2.size() < 50) {
    int index = rnd.nextInt(list1.size());
    list2.add(list1.remove(index));
}

UPDATE:

BTW: index is an int, not an Integer, so list1.remove(index) removes the element at given index, and not the element with given value.

Comments

0

I suggest you simply shuffle the original list.

List<SomeObject> objects = new ArrayList<>();
Collections.shuffle(objects);

Now, take the value from the end (more efficient for ArrayLists), and then delete it.

int last = objects.size()-1;
while (last >= 0) {
     SomeObject ob = objects.get(last);
     objects.remove(last--);
     // now do whatever you want with ob
}

Comments

0

Please pay attention to the line #7:

Random rnd = new Random();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
    
while (list2.size() < 50) {
    int index = list1.get(rnd.nextInt(list1.size()));
    list2.add(index); <-- you are adding random index here instead of list1's value
    list1.remove(index);
}

Comments

0

Finally I got the issue solved by shuffling the list1 object before putting them to list2

Collection.shuffle(list1);
for (int i=0; i<=50; i++){
    list2.put(list1(i);
}

This gave me exactly what i wanted. Thanks everyone for your contributions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.