-2
int[][] array = new int[5][5];
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length; j++) {
            System.out.print(1);
        }
        System.out.println();
    }

Hi guys, i have this array which prints a 5x5 1's

11111
11111
11111
11111
11111

What i would like to do is to make three (3) of those 1's as 0 randomly. Example

11101
11111
11011
10111
11111

How do I do that? Thank you in advance!

0

3 Answers 3

1

You would have to firstly initialize the array with 1s:

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

Then set 0s in 3 random positions:

Random random = new Random();
int a = 0;
while (a < 3) {
    int i = random.nextInt(array.length);
    int j = random.nextInt(array[0].length);
    if(array[i][j] != 0) {
        array[i][j] = 0;
        a++;
    }
}

Using this approach each entry in the array has the same chance of becoming a 0. Finally you should print the array:

for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length; j++) {
        System.out.print(array[i][j]);
    }
    System.out.println();
}
Sign up to request clarification or add additional context in comments.

Comments

1

For random stuff, you can use Math.random(). It returns a random double between 0 included and 1 excluded. You could do this :

int w = 5, h = 5;
int[][] array = new int[w][h]; 
for (int i = 0; i < w; i++) { 
  for (int j = 0; j < h; j++) {  
    array[i][j] = 1;
  }  
}
for (int c = 0; c < 3; c++) {
  int i, j;
    do {
      int index = (int)(Math.random() * w * h);
      i = index / w;
      j = index % w;
    } while (array[i][j] == 0);
    array[i][j] = 0;
  }

This solution is acceptable only if the number of 0 is low compared to the total size of the array. If the number of 0 is too high this code is going to be very inefficient because we will search randomly for a value of 1

2 Comments

This answer might not be correct as at least 49% of 0s distribution is uniform, therefore I wouldn't consider it as random... Trailing 0's 111....000, 11..000...111 etc...
This version should do it normally, am I wrong ? @MS90
1

Here is a slightly different solution for fun. Create an list with numbers from 0 to 24, shuffle the list and pick the the 3 first elements from it to update the 2D array. The sequence of numbers means that not the same position in the array will be set to 0 more than once.

List<Integer> sequence = IntStream.rangeClosed(0, 24)
    .boxed()
    .collect(Collectors.toList());

Collections.shuffle(sequence);

for (int i = 0; i < 3; i++) {
    int value = sequence.get(i);
    int row  = value / 5;
    int  column = value % 5;

    array[row][column] = 0;
}

The code for generating the sequence came from this answer

1 Comment

You shouldn't remove 1 to column because your numbers are already in a 0-based index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.