Question

So im filling an array with random numbers from 1-10 by using a second array to fetch the numbers from. I need to get 10 different results, each run outputting a different random array list. How could i do that? Right now if i try to output it more than once, it just out puts the same numbers over and over again. I need 10 different outputs.

public static void main(String[] args) {
    List<Integer> numbers =new ArrayList<Integer>();
    List<Integer> numbers2 =new ArrayList<Integer>();

    Random generator = new Random();


    for(int i = 0; i<=10;i++) {
        numbers2.add(i);
    }

   for(int z = 0; z<=10; z++){

    for(int i = 0; i<=1000;i++) {

        int x = generator.nextInt(10)+1;
        if (!numbers.contains((numbers2.get(x)))) {
            numbers.add(numbers2.get(x));;
        }
    }
    System.out.println(numbers);
}

}

Was it helpful?

Solution

You are using the same numbers array at each of the 10 iterations. By the time the first iteration is over, the array already has every digit from 1 to 10 so the if condition will evaluate to false and nothing will be added to it.

So you need to create a new numbers array for each of the 10 iterations, or remove its contents after printing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top