2

I made a method that uses the random class to generate a number. These numbers are used to fill in an array. This method returns the filled in array. So, I have a few other arrays that I made which are equal to the returned array. However, these arrays are all the same even though they should be random.

I tried printing how each array that calls the random, right under where it calls. This gave me different arrays. But if I move my prints to under all the calls then the arrays are all the same. Also I think this is an array issue because I only have this problem with arrays.

    public static int[] numberGenerator(int[] array) {
        Random rand = new Random();
        int min = 0;
        int max = 0;
        int x = 0;

        for (int j = 0; j < array.length; j++) {        

            if (j == 0) {
                min = 1;
                max = 15;
            }
            if (j == 1) {
                min = 16;
                max = 30;
            }
            if (j == 2) {
                min = 31;
                max = 45;
            }
            x = rand.nextInt((max - min) + 1) + min;
            array[j] = x;                               
        }
        return array;
    }

    public static void main(String[] args) {
        int[] array = {' ', ' ', ' '};
        int[] array1 = numberGenerator(array);  
        int[] array2 = numberGenerator(array);

        System.out.println(array1[1]);
        System.out.println(array2[1]);          
    }
}

Distinct arrays are what I'm looking for.

Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • try adding print statements in between the method calls, see if that tells you more about where the problem lies – Stultuske Jan 22 '19 at 07:19
  • Use Debugger if you are using any IDE such as Eclipse. It will save a lot of time of yours (and stackoverflow users as well.) Happy Coding and welcome to the world of stackoverflow. – Prashant Zombade Jan 22 '19 at 07:23
  • Maybe have a look at: [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Lino Jan 22 '19 at 07:30

4 Answers4

4

You're modifying the array that you pass into the method. Given that you don't use the existing content of the array anyway, it would be better to just pass in the length that you want, and create a new array in the method:

public static int[] numberGenerator(int length) {
    int[] array = new int[length];
    // Rest of method as before
}

Then change your main method to:

public static void main(String[] args) {
    int[] array1 = numberGenerator(3);  
    int[] array2 = numberGenerator(3);

    System.out.println(array1[1]);
    System.out.println(array2[1]);          
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

You have to understand how pass by reference and pass by value work in Java. Arrays are considered as objects, and they get passed into methods by reference. This means, in your code there is only one physical array array and that is getting passed along, and all the modifications are being done on it.

You can follow any of the designs suggested by other answers to solve your problem!

Andy Sug
  • 234
  • 2
  • 7
1

You are passing the same array object to the two calls to numberGenerator, so of course they return the same array (since numberGenerator doesn't create a new array, it just fills the array you pass to it).

You can pass a different array in each call:

int[] array1 = numberGenerator(new int[3]);  
int[] array2 = numberGenerator(new int[3]);

Or, as an alternative, pass an array length to numberGenerator, and let that method create the array:

public static int[] numberGenerator(int length) {
    int[] array = new int[length];

    ... fill the array ...

    return array;
}

and use it with:

int[] array1 = numberGenerator(3);  
int[] array2 = numberGenerator(5);
Eran
  • 387,369
  • 54
  • 702
  • 768
0

Don't use the passed parameters to store the generated random number. You should create a new array in the method to store the random number. This is because the argument to the array type of the method is a reference. The array you return is actually a reference to this array. The value of this array depends on your last modification. The value of array1 above will become the last modification. This is why array1 is the same as array 2 because they all point to an array.

public static int[] numberGenerator(int[] array) {
    Random rand = new Random();
    int min = 0;
    int max = 0;
    int x = 0;
    int[] newArray = new int[array.length];
    for (int j = 0; j < array.length; j++) {

        if (j == 0) {
            min = 1;
            max = 15;
        }
        if (j == 1) {
            min = 16;
            max = 30;
        }
        if (j == 2) {
            min = 31;
            max = 45;
        }
        x = rand.nextInt((max - min) + 1) + min;
        newArray [j] = x;
    }
    return newArray ;
}
hkuan
  • 1