hank99
hank99

Reputation: 185

Generate a random 2d array with random row length

I am trying to generate 2d random array similar to:

 array[random][random2]
 { 
   {1, 2},
   {6, 4},
   {-1, 5},
   {-2}
 }

the values in the array are also random, It may have -9 to -1 to 1 to 9 numbers.

Here's what i got:

public class gen2dArray {

   public static void main(String args[]) {

      Random random = new Random();
      int n = 0;
      int max = 5, min = 1;

      n = random.nextInt(max - min + 1) + min;

      int maxRowCol = (int)Math.pow(2,n);

      int RandMaxRows = random.nextInt(maxRowCol);
      int RandMaxColums = random.nextInt(maxRowCol);


      int [][] array = new int [RandMaxRows][RandMaxColums];

      for (int i = 0; i < array.length; i++) {
         for (int j = 0; j < random.nextInt(array[i].length)+1; j++) {
           array[i][j] = random.nextInt(9) + 1;
         }
      }
      System.out.println(Arrays.deepToString(array));
   }
}

Output 1:

 [
   [4, 4, 5, 0], 
   [2, 3, 0, 0]
 ]

Output 2:

[
   [5, 2, 1, 0, 0],
   [3, 4, 2, 0, 0],
   [3, 1, 5, 0, 0, 0], 
   [4, 3, 2, 0, 0]
 ]

There are few problems,

  1. Some outputs are just [[]] or []

2.

Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
            at java.util.Random.nextInt(Random.java:300)
            at gen2dArray.main(gen2dArray.java:23)
  1. Getting the zeros in the array. There should be no zeros.

Output one has to be like:

[
   [4, 4, 5],
   [2, 3]
]

Upvotes: 0

Views: 1265

Answers (2)

YoungHobbit
YoungHobbit

Reputation: 13402

You can simulate all these scenario by putting a System.out statement just before the array declaration.

System.out.println("RandMaxRows: " + RandMaxRows + "\tRandMaxColums: " + RandMaxColums);

  1. Some outputs are just [[]] or []

This is happening when you are getting RandMaxRows as zero or both row and column are zero. We are using the Random.nextInt(n) for declaring the size of the array. The range of this method is 0 to n-1. So you can fix this by increment the outcome of the method.

  1. n must be positive

This is happening when the RandMaxColums is zero. The input to the random.nextInt(n) must be greater then 0, it throws the exception.

  1. Getting the zeros in the array. There should be no zeros.

The inner for-loop should be like this.

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

You need iterate the array with it's length, for that any random number generation is not required. Use the current length and fill the array with randomly generated numbers.


Here is the work code for your problem:

public class gen2dArray {

  public static void main(String args[]) {

    Random random = new Random();
    int n = 0;
    int max = 5, min = 1;

    n = random.nextInt(max - min + 1) + min;

    int maxRowCol = (int)Math.pow(2,n);

    int RandMaxRows = random.nextInt(maxRowCol);
    int RandMaxColums = random.nextInt(maxRowCol);

    System.out.println("RandMaxRows: " + RandMaxRows + "\tRandMaxColums: " + RandMaxColums);
    int [][] array = new int [RandMaxRows+1][RandMaxColums+1];

    for (int i = 0; i < array.length; i++) {
      for (int j = 0; j < array[i].length; j++) {
        int temp = random.nextInt(9);
        array[i][j] = temp + 1;
      }
    }
    System.out.println(Arrays.deepToString(array));
  }
}

Sample Output:

RandMaxRows: 3  RandMaxColums: 3
[[1, 3, 3, 3], [2, 7, 6, 1], [5, 4, 4, 1], [6, 4, 6, 9]]

Upvotes: 0

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

If you do not want to see the zeros, you should avoid making your arrays to big :

  int [][] array = new int [RandMaxRows][];
  // Code
  array[value] = new int[random.nextInt(maxRowCol)];

This method should provide you an array of differents arrays of different sizes and just print out their real inputted values and not the 0 default values.

This is an example of an output :

[
    [2, 4, 8, 7, 6, 6, 9], 
    [1, 3, 4, 2], 
    [1, 4, 4, 2, 7, 6, 8], 
    [9, 3, 6, 3, 7, 3], 
    [4, 5, 3, 2, 5, 2, 8]
]

Here is the modified code :

int [][] array = new int [random.nextInt(maxRowCol)][];

      for (int i = 0; i < array.length; i++) {
         array[i] = new int[random.nextInt(maxRowCol)];
         for (int j = 0; j < array[i].length; j++) {
           array[i][j] = random.nextInt(9) + 1;
         }
      }

When [] is printed, it means your Random did return 0.

A simple way to fix this is to add 1 to the concerned value and maybe decrease the MAX value from one also to keep the logic going.

Here is the code :

int maxRowCol = (int)Math.pow(2,n) - 1;
// Some code
int[][] array = new int [RandMaxRows + 1][];

Upvotes: 1

Related Questions