1

So this is what I have so far.

public static int[][] generateRandomPositions(int number, int width, int height){

    for(int j=0; j <number; j++){
         int[][] pos = new int[][]{
             {Utility.randomInt(width),Utility.randomInt(height)}

         };

    }

    return [][]pos;
}

Basically the method gets a number which is the number of rows there should be and the width and height is the two numbers that will be in the two columns. Those of which are randomly generated between the number given (ex Utility.randomInt(5) would be between 0 and 5). The problem I am having is figuring out how to create the number of rows based off the number that is inputted. What I have I don't believe works. This is an example of what the out come should be if these numbers were inputted.

generateRandomPositions(4, 5, 30)
int[][] posB = new int[][] {
{ 3,21 }, 
{ 4,15 },
{ 1,17 }
{ 3,9 }
};

There are 4 rows because 4 was inputted as the number. The other numbers were randomly generated. So I just need help figuring out how to create the number of rows based off the numbers variable inputted. I am fairly new to programming so and suggestions and help would be greatly appreciated.

6

1 Answer 1

0
public static int[][] generateRandomPositions(int number, int width, int height){
    int[][] pos = new int[number][2];
    for(int j=0; j <number; j++){
       pos[j][0] = Utility.randomInt(width);
       pos[j][1] = Utility.randomInt(height);
    }
    return pos;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, 2d arrays just really confuse the heck out of me.

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.