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.

3
  • 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. Commented Jan 7, 2023 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! Commented Jan 7, 2023 at 14:43
  • For more ways of shuffling a list of numbers from 1-n, you can also look at stackoverflow.com/a/4262134/908821 Commented Jan 7, 2023 at 15:03

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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;
        }
    }
}

Comments

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.