1

I am trying to program a Java Code in which, given the lenght of an int array and a min and max values it returns an random array values.

I am trying to program it in the most simple way, but I am still not getting how the programming process works.

What I have tried is the following:

import java.util.Random;
public class RandomIntArray {
    public static void main(String[] args){

        System.out.println(createRandom(10));
    }
    public static int[] createRandom(int n) {
        Random rd = new Random();
        int[] array = new int[n];
        int min = 5;
        int max = 99;

        for (int i = 0; i < array.length; i++) {
            array[i] = rd.nextInt();
            while (array[i] > min) ;
            while (array[i] < max) ;

        }
        return array;
    }

}
Maria
  • 47
  • 1
  • 7

3 Answers3

1

Try using IntStream to achieve what you want

public static void main(String[] args) {
    int[] arr = createRandom(10);
    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
}

public static int[] createRandom(int n) {
    Random r = new Random();
    int min = 5;
    int max = 99;
    return IntStream
            .generate(() -> r.nextInt(max - min) + min)
            .limit(n)
            .toArray();
}
IhorK
  • 11
  • 1
0

The first thing we must adjust is the random number generation. It appears you specify a min and max, so a number must be generated in with these bounds. We can use the Random.nextInt(int bound) method to set a range equal to max - min. However, this range will start from 0, so we must add your min value to make the bound complete. Using this process will eliminate the need for the existing while loops. See this question for more on this.

Now, every value in the array will be generated as

array[i] = rd.nextInt(max - min) + min

When trying to print array elements, a System.out.println() statement with the variable name of the array will print a memory address of the array. This will NOT print the entire array of values.

We must instead iterate over the elements to print them. It would be best to use a for loop to do this, but your program could generate an array of any size. So, we should use a for-each loop instead as follows:

for (int num : createRandom(10)) { 
   System.out.println(num); 
}

This for-each is saying "for each int in the array returned by createRandom(10), refer to is as variable num and print it."

This is the final code:

import java.util.Random;
public class RandomIntArray {
   public static void main(String[] args){
      for (int num : createRandom(10)) { 
         System.out.println(num); 
      }
   }
   public static int[] createRandom(int n) {
      Random rd = new Random();
      int[] array = new int[n];
      int min = 5;
      int max = 99;

      for (int i = 0; i < array.length; i++) {
         array[i] = rd.nextInt(max - min) + min;
         System.out.println(array[i]);
      }
      return array;
   }
}
Paul
  • 137
  • 4
0
public static int[] createRandom(int n) {
    Random rd = new Random();
    int[] array = new int[n];
    int min = 5;
    int max = 10;

    for (int i = 0; i < array.length; i++) {
        array[i] = rd.nextInt(max-min+1) + min;
        System.out.print(array[i] +" ");
    }
    return array;
}

public static void main(String[] args){
    createRandom(5);
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
HassanYu
  • 55
  • 3
  • 11
  • Thanks a lot for your help! I have a question regarding your code: what does this bit mean: array[i] = rd.nextInt(max-min+1) + min;? – Maria Nov 20 '20 at 09:30
  • if your min is 5 and your max is 10,Then (max - min + 1) + min gives you 11,means rd.nextInt(11) and give you numbers 0 ≤ number< 11. – HassanYu Nov 20 '20 at 09:41