Another approach to fill your grid with 3 random Os would be to do it right upon initialization:
int numOs = 3;
for(int row = 0; row<grid.length; row++) {
for(int col = 0; col<grid[row].length; col++) {
if( numOs > 0 && randomConditionForO() ) {
grid[row][col] = 'O';
numOs--;
} else {
grid[row][col] = 'N';
}
}
}
randomConditionForO() could be anything like rn.nextInt() % 2 == 0 etc. If you want to provide a probability for setting an O, let's say 25%, you could use something like rn.nextInt(100) < 25 or rn.nextFloat() < 0.25f.
The problem here, however, would be that you could get less than 3 Os due to the randomness off the condition so you might want to counter that. One way might be to provide a count for the Ns as well and increase probability for an O accordingly.
Example:
int numOs = 3;
int numNs = gridSize - numOs; //gridSize would be 3x3 = 9 in your example
for(int row = 0; row<grid.length; row++) {
for(int col = 0; col<grid[row].length; col++) {
if( numOs > 0 && rn.nextInt(numNs + 1) < numOs ) {
grid[row][col] = 'O';
numOs--;
} else {
grid[row][col] = 'N';
numNs--;
}
}
}
Here rn.nextInt(numNs + 1) < numOs would mean that the probability to chose an O increases with each chosen N and decreases with each chosen O. Since nextInt(bound) will return an integer between 0 (inclusive) and bound (exclusive) we need to pass in numNs + 1 so that if there are no more Ns available we use the bound 1 and since it is exclusive we'll always get 0, which is smaller than numOs as long as there are Os available (thus getting 100% probability).
grid[row].length. And @Eran answer is the good oneclosed thread- solvedis not an acceptable way to mark that your question was answered. If you got the answer you were looking for, you should mark that answer as accepted (click the check mark next to it).