3

This Code creates 50 random-numbers between 1 and 100 and adds it in an ArrayList. Now I want to search the ArrayList for the same numbers, than remove them and get a new number. In the end, the list should only contain 50 unique numbers between 1 and 100.

The Problem is: I don't know, how to search the same ArrayList for the same number, remove it and get a new one. Can someone please help me?

import java.util.Random;
import java.util.ArrayList;

class RandomPrim {

    public static void main(String[] args) {

        Random nr = new Random();
        int number;
        ArrayList<Integer> liste = new ArrayList<Integer>();

        // get 50 random numbers between 1 and 100
        for(int counter = 1; counter <= 50; counter++) {
            number = 1+nr.nextInt(100);
            liste.add(number);

            // System.out.println(liste.toString());
        }

        for (int ausgabe : liste) {
            System.out.print(ausgabe+ ", ");
        }
    }
}
3
  • 4
    Why to don' you use Set instead? Commented Jun 30, 2016 at 12:58
  • As @gevorg said you can use a HashSet and add random numbers to it. You can stop when its length is 50. Commented Jun 30, 2016 at 13:00
  • 1
    Another approach could be to create a list of 100 numbers and randomly take one of the elements out of that list, i.e. you'd get the next index by calling nextInt( availableNumbers.size() ) (ofc you should remove any number you took from that list). Commented Jun 30, 2016 at 13:02

5 Answers 5

3
Random nr = new Random();
int number;
ArrayList<Integer> liste = new ArrayList<Integer>();

// get 50 random numbers between 1 and 100
for(int counter = 1; counter <= 50; ) {
    number = 1+nr.nextInt(100);
    if(!(liste.contains(number))) {
        liste.add(number);
        counter++;
    }
}

for (int ausgabe : liste) {
    System.out.print(ausgabe+ ", ");
}

hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

Better use HashSet instead, to avoid duplications:

Random nr = new Random();
int number;

Set<Integer> randomSet = new HashSet<>();

// get 50 random numbers between 1 and 100
while(randomSet.size() < 50) {
    number = 1 + nr.nextInt(100);
    randomSet.add(number);
}

for (int ausgabe : randomSet) {
    System.out.print(ausgabe + ", ");
}

1 Comment

unfortunally I have to use an ArrayList or LinkedList
2

As already has been suggested using a Set or, if insertion order is important, a LinkedHashSet and keep generating random numbers until the set has a size of 50.

The problem with that, however, might be that as the set fills up you could get more and more duplicates thus requiring a lot of retries.

So an alternative could be to use a list of 100 numbers and then randomly take one out:

List<Integer> availableNumbers = new ArrayList<>( 100 );
for( int i = 1; i <= 100; i++ ) {
  availableNumbers.add( i );
}

Random r = new Random();
List<Integer> randomizedList = new ArrayList<>( 50 );
for( int i = 0; i < 50; i++ ) {
  int randomIndex = r.nextInt( availableNumbers.size() );
  randomizedList.add( availableNumbers.remove( randomIndex ) );
}

Note that using an ArrayList has the drawback that if you take out a number all numbers following it would have to be shifted to the left. On the other hand using a LinkedList would require an iteration to reach the index of the element that should be removed.

Comments

1

You could use a linq query for this:

liste.Distinct().ToArray();

This should at least get you headed in the right direction.

Comments

1

If you don't want to try any of the other solutions, you can just get unique numbers from the beginning while still using an ArrayList like so..

Random nr = new Random();
int number;
ArrayList<Integer> liste = new ArrayList<Integer>();

// get 50 random numbers between 1 and 100
for(int counter = 1; counter <= 50; ) {
    number = 1+nr.nextInt(100);
    if(!(liste.contains(number))) {
        liste.add(number);
        counter++;
    }
    else {
        counter = counter - 1;
    }
}

for (int ausgabe : liste) {
    System.out.print(ausgabe+ ", ");
}

1 Comment

This will use recursion - how so? I don't see any recursive call. Also counter = counter - 1; might result in an endless loop.

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.