Hi i need to change the random numbers in my output to random 'b''s that take up 2/3 of the array and random 'x''s to take up 1/3. Ive been at it for a few days and still have no clue on what to do. Heres my coding and output:

import java.util.Random;
public class Board
{
	/**
	 * Create Board
	 */
	final int rowWidth = 10;
    final int colHeight = 10;
	int [][] board = new int [rowWidth][colHeight];
	Random rand = new Random();
	
	/**
	 * Display the board
	 */
	public void getBoard()
	{
		for (int row = 0; row < board.length; row++) 
		{
			for (int col = 0; col < board[row].length; col++) 
			{
				
				board[row][col] = rand.nextInt(10);
		
				
            }
		}
		//display output
        for(int i = 0; i < board.length; i++) {

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

}

Output:
2 0 1 0 7 8 7 7 7 4
2 3 0 1 5 9 9 3 6 1
9 8 7 8 1 1 2 7 6 4
5 8 0 1 8 1 3 3 8 9
2 5 5 2 9 6 9 3 6 4
3 9 2 5 5 1 2 6 3 0
2 9 0 3 0 0 9 4 9 1
8 4 0 2 8 6 0 4 8 6
4 5 6 1 9 0 8 2 8 8
1 2 2 0 5 9 1 6 2 4

thanks

Recommended Answers

All 7 Replies

loop the array and call the rand.nextInt(3). You use 3 because you said you want 1/3 and 2/3. If the result is 0 then put 'x' in the array. If it is 1 or 2 then put 'b'.
Of course the array would be an array of chars

alright, i gotcha on the rand.nextInt(3) and how that'll work, but im having trouble looping my array right now, where should i loop it, before my rand.nextInt, or after?

What do you intend to do with the value of the random. Aren't you going to populate the array?

yah, i need to populate the array with b's and x's but im having trouble assigning them to the 0's 1's and 2's. heres my output:
1 1 0 2 1 2 2 1 1 1
2 0 2 0 1 1 1 2 2 0
1 2 1 2 1 0 1 1 0 1
0 2 2 0 2 0 1 2 2 1
2 0 0 1 2 2 0 0 2 1
1 0 2 1 1 2 1 2 1 2
2 0 1 0 2 2 2 2 2 2
0 0 1 2 1 2 0 0 1 2
0 1 1 0 0 1 2 2 2 0
1 0 0 0 2 2 2 1 1 1

but i need it to be the letters (x and b)

Then you need to put the 'x' and 'b' into an array.

so you mean create another 10X10 and put x and b into it or? (im kinda new at java and this being my 3rd lab im kinda confused so sorry if im annoying you)

so you mean create another 10X10 and put x and b into it or? (im kinda new at java and this being my 3rd lab im kinda confused so sorry if im annoying you)

You need to start thinking on your own.
Can you put char inside an int array? No.

Also you haven't explained your problem properly. Since you say that you need to randomly put characters into an array you should have thought that you needed a char array. So what the code posted has to do with anything? Maybe it was an example needed modifying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.