0
`import java.util.Random;

public class Main {

    public static void main(String[] args) {
        int[][] board = new int[5][5];
        int sNA = 5;

//        new GUI();

        sequenceMaker(board);
        drawBoard(board);
    }
//
    public static void drawBoard(int[][] board2D) {
        int m=1;

        for(int i = 0; i < board2D.length; i++){
            for(int j=0; j < board2D.length; j++){
                System.out.print(board2D[i][j]+" ");
            }
            System.out.println();
        }
    }

    public static void sequenceMaker(int[][] board2D) {
        Random rand = new Random();
        int m = 1;
        int x = 0;

        while(x < 24){
            int columnRandom = rand.nextInt(5);
            int rowsRandom = rand.nextInt(5);
            x += 1;

            if(board2D[columnRandom][rowsRandom] == 0) {
                board2D[columnRandom][rowsRandom] = m;
                m += 1;
            }
            else if(board2D[columnRandom][rowsRandom] == m) {
                while(board2D[columnRandom][rowsRandom] == m) {
                    columnRandom = rand.nextInt(5);
                    rowsRandom = rand.nextInt(5);

                    board2D[columnRandom][rowsRandom] = m;
                    m+=1;
                }
            }
        }
    }
}`

This is what I wrote to this point, but the output doesn't include every index, maximally going to 15-16ish.

I tried a while loop, so that if another integer is in the place of randomally generated number, it generates those number once again. I don't know why my output is incomplete though.

  • Okay, while changing the while(x < 24) to while(x < 100) it started working? I don't know why this approach works, but I think my code still has a big flaw. – Mikolaj Ciuraba Jan 07 '23 at 14:40
  • Nevermind, I have figured it out. I have moved the x+=1 into the if statements and everything works perfectly. Case solved! – Mikolaj Ciuraba Jan 07 '23 at 14:43
  • For more ways of shuffling a list of numbers from 1-n, you can also look at https://stackoverflow.com/a/4262134/908821 – Angel Koh Jan 07 '23 at 15:03

2 Answers2

1

I have solved my own question (I think).

What I have changed is that in the sequenceMaker() function, I have moved the x+=1 into the if statements. The code looks like this :

public static void sequenceMaker(int[][] board2D) {
    Random rand = new Random();
    int m = 1;
    int x = 0;

    while(x < 25){
        int columnRandom = rand.nextInt(5); // random column
        int rowsRandom = rand.nextInt(5); // random row

        if(board2D[columnRandom][rowsRandom] == 0) {
            board2D[columnRandom][rowsRandom] = m;
            m += 1;
            x += 1;
        }
        else if(board2D[columnRandom][rowsRandom] != 0) {
            while(board2D[columnRandom][rowsRandom] == 0) {
                columnRandom = rand.nextInt(5);
                rowsRandom = rand.nextInt(5);

                board2D[columnRandom][rowsRandom] = m;

                m+=1;
                x += 1;
            }
        }
    }
}

I hope my mediocrity helped someone!

0
public static void sequenceMaker(int[][] board2D) {
    //it will initialize your array with random integers 
    Random rand = new Random();

    for(int i=0; i<board2D.length; i++){
        for(int j=0; j < board2D[i].length; j++ ){
            int randomNo = rand.nextInt(20); //you can change the bound as required
            board2D[i][j] = randomNo;
        }
    }
}
Fahad Khan
  • 36
  • 4