- Home /
Iterating through shuffled/randomized array issue
Basically I am trying to create a sudoku grid generator, and am having some sort of issue with the proper operation to "shuffle" an array's values to another array's indices in a for loop. I originally tried to brute force each column and row and then box (subgrid) on a sudoku board, but then found a neat algorithm here. Seems easy enough to follow, but when coding it in C#, after making a fisher-yates shuffle (I haven't found any implicit shuffle methods in Unity C#) it doesn't shuffle at the right time. The Shuffle() randomizes the array without duplicates, and the GenerateGrid() returns the proper values for the first subgrid (0, 1, 2, 9, 10, 11, 18, 19, 20), and those thereafter, but for some reason, the Shuffle is performed before attributing the values to the proper indices of the array? Everything shows fine when I debug.log each function individually. It shows that by the time it evaluates indices 9, 10, and 11 that it has Shuffle()d again, but those values should have been set by the time they were evaluated by the console. Here's the code.
public void Shuffle() {
for (int i = 0; i < arr.Count; i++) {
int tempNum = arr[i];
int k = Random.Range(i, arr.Count);
arr[i] = arr[k];
arr[k] = tempNum;
} // This is the fisher-yates shuffle. Works fine in practice by itself. Just used to iterate randomly through an array without duplicates
}
public void GenerateGrid() {
for (int i = 0; i < 81; i++) {
if (i % 9 == 0) {
Shuffle(); // every 9 iterations changes the order of arr as shown above.
}
int perBox = (((i / 3) % 3) * 9) + (((i % 27) / 9) * 3) + ((i / 27) * 27) + (i %3); // this is an equation for getting var perBox to equal each subgrid's (3*3 square) array values. Works fine by itself in practice returning first the [0,1,2,9,10, 11...] as previously stated.
grid[perBox] = arr[i%9]; // should iterate through 0 - 9 before shuffling and assign each subgrid array's values to arr[i] before arr[i] gets changed in Shuffle().
} //GenerateGrid() called on awake, start, or getkeydown/update. Doesn't seem to change the outcome.
}
I am assuming I'm missing something simple. Sorry if that's the case, I just don't see what I'm missing.
I copied and pasted your code and it works for me. It shuffles at the start, then again after box 20, then after box 23, ect. Basically placing 1-9 in rows (or columns).
0 1 2 9 10 11 18 19 20 Shuffle
3 4 5 12 13 14 21 22 23 Shuffle
6 7 8 15 16 17 24 25 26 Shuffle
Figured out the issue. You're right, the code does work. I checked it in the same for loop in GenerateGrid after the shuffle with a debug.log for i where i = 0, 1, 2, 9, 10, 11... I did if (i < 3) { Debug.Log(i + " " + grid[perBox]); } if (i >= 9 && i < 12) { Debug.Log(i + " " + grid[perBox]); } if (i >= 18 && i < 21) { Debug.Log(i + " " + grid[perBox]); }
the issue being in the value of perBox being dependent on i. I ran through all 81 indices in a seperate for loop and found it worked exactly as I intended. Is there a way to mark this as solved or just delete it? I feel bad the issue wasn't even in the code I originally posited.