-1

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • The n value is never greater than the min value because the modulus operator gives you remainder when you divide the number by 10, you get a value less than 10. – bjorke07 Mar 26 '22 at 12:18
  • Hint: your loop iterates ONCE over your array. But you have a condition in there that REJECTS some elements. So: you need a different kind of loop. You can only progress to the next element of your array once you found a VALID answer. Also not that the `100%s` is not necessary. The Random class has methods that allow you to directly retrieve random ints in a specific interval. – GhostCat Mar 26 '22 at 12:25
  • See https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java for example. – GhostCat Mar 26 '22 at 12:25
  • unfortunately I must use this format... this is an intro course to JAVA and I cannot use stuff I did not learn in class or else I will not be awarded the whole grade @GhostCat – Charbel Nicolas Mar 26 '22 at 12:37

1 Answers1

1
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));
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28