[SOLVED] Method for array of given size containing random integers between given min and max

Issue

I have to do this in Java:

Write a method fillArray() that takes three integers: (s, min, max),
and returns an array of size s having random integers with values
between min and max.

This is the code I wrote

public static void fillArray(int s, int min, int max) {
    int[] random = new int[s];

    for (int i = 0; i < s; i++) {
        int n = (int) (Math.random()*100 %s);
        if (n > min && n < max) {
            random[i] = n;
        }
    }

    System.out.printf("Here's an array of size %d, whose elements vary between %d and %d: \n", s, min, max);
    System.out.print(Arrays.toString(random));
}

The problem is, when I implement my method in the main, fillArray(10, 10, 20), it gives me arrays of size 10, with elements at 0.

I tried playing around with this specific expression in the code

int n = (int) (Math.random()*100 %s);

and changing what I do after *100.

Sometimes it works for most elements, but I still get some elements which are 0, which is wrong since the minimum is 10.

Any idea how I could fix that?

Solution

Math.random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

So since the random numbers you generate are always between [0 - 1) (Zero inclusive & 1 exclusiv you need to multiply them by your desired max number to generate random numbers which are greter than one. Example to generate random numbers between [0 - 15) you can do

Math.random() * 15

to get values between [0.0 - 14.999..].

To include 15 in your values you need to multiply with (15 + 1) = 16

Math.random() * (15 + 1) or generaly
Math.random() * (max + 1)

Since there is also a min value in your requierment, you need to exclude values between 0 - min. To do so you could add simply the min value to the result of the above:

(Math.random() * (max + 1)) + min

But wait, let see an example for min = 5 and max = 15. Since

Math.random() * (max + 1)

returns values between [0.0 - 15.999..] adding min = 5 to each value:

(Math.random() * (max + 1)) + min

will result in values in range [5.0 - 20.999..] which is not desired. To change that you will need to substract min from max while multiplying:

(Math.random() * (max - min + 1)) + min

which will result in the correct range [5.0 - 15.999..] where you need to apply casting (int) to get your random integers instead of dobles. So your method could look like

public static void fillArray(int s, int min, int max) {
    int[] random = new int[s];

    for (int i = 0; i < s; i++) {
        int n = (int) (Math.random() * (max - min + 1)) + min;
        random[i] = n;
    }

    System.out.printf("Here's an array of size %d, whose elements vary between %d and %d: \n", s, min, max);
    System.out.print(Arrays.toString(random));
}

Answered By – Eritrean

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *