I've been trying to figure this out all night. I started on this sudoku program and for some reason it's in an infinite loop. Basically, I'm using Math.random() to generate integers and collecting them into a two dimensional array that is 9 X 9. Then I use a method to verify that the numbers in the array are unique by row and by column (and soon to be box) if I can figure this out. It seems to work just fine when I only check either row or column at a time, but not both. I debugged and it seems to be doing what it's supposed to be doing by breaking away from the for loop when the value equals a value already in its row or column, then it generates a new random integer, and goes back.. but I can't tell why it says it changes, but gets stuck on the same value.
public class TestSudokuLogic { public static int counter = 0; public static void main(String[] args) { //create grid int[][] grid = new int[9][9]; for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { boolean valid = false; while (!valid) { grid[row][col] = getRandomInt(); valid = checkRandomInt(grid, row, col); counter++; System.out.println(counter); } } } printArray(grid); } public static boolean checkRandomInt(int[][] grid, int row, int col) { for (int i = 0; i < col; i++) if (grid[row][col] == grid[row][i]) //checks each col in row (rows are unique 1-9) return false; for (int j = 0; j < row; j++) if (grid[row][col] == grid[j][col]) //checks each row in col (cols are unique 1-9) return false; return true; } public static int getRandomInt() { int r = 0; while(r < 1) r = (int) (Math.random() * 10); return r; } public static void printArray(int[][] a) { for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[row].length; col++) System.out.print(a[row][col] + " "); System.out.println(); } } }