My original efforts were in trying to generate random sudoku puzzles using lists. I want to make sure that while the sequences don't duplicate across (a "row"), they also don't duplicate vertically (like a column). Considering the use of lists as the sequence, the strategy was to use indexes to compare and "re-roll/-shuffle" any row, should it be found any indexes shared value.
This is the initial set up:
import random
row1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row2 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row3 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row4 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row5 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row6 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row7 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row8 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
row9 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
random.shuffle(row1)
The two following, smaller (9x3) attempts are equivalent. The first uses the specific index numbers, while the second uses a variable and range to iterate the process instead. They both seem to function fine, without having any element of the list sequence duplicating any other.
# Random Nonduplicate Sequence Generator 1
print("\n")
while row2[0] == row1[0] or row2[1] == row1[1] or row2[2] == row1[2] or row2[3] == row1[3] or row2[4] == row1[4] or row2[5] == row1[5] or row2[6] == row1[6] or row2[7] == row1[7] or row2[8] == row1[8]:
random.shuffle(row2)
while (row3[0] == row1[0] or row3[1] == row1[1] or row3[2] == row1[2] or row3[3] == row1[3] or row3[4] == row1[4] or row3[5] == row1[5] or row3[6] == row1[6] or row3[7] == row1[7] or row3[8] == row1[8]) or (row3[0] == row2[0] or row3[1] == row2[1] or row3[2] == row2[2] or row3[3] == row2[3] or row3[4] == row2[4] or row3[5] == row2[5] or row3[6] == row2[6] or row3[7] == row2[7] or row3[8] == row2[8]):
random.shuffle(row3)
print(row1)
print(row2)
print(row3)
# Random Nonduplicate Sequence Generator 2
print("\n")
for x in range(0, 8):
while row2[x] == row1[x] or row2[x] == row3[x]:
random.shuffle(row2)
while row3[x] == row1[x] or row3[x] == row2[x]:
random.shuffle(row3)
print(row1)
print(row2)
print(row3)
However, when I attempt to expand upon this to encompass a whole 9x9 "grid", I start getting duplicates:
# Random Nonduplicate 9x9 Generator 1
print("\n")
for x in range(0, 8):
while row2[x] == row1[x] or row2[x] == row3[x] or row2[x] == row4[x] or row2[x] == row5[x] or row2[x] == row6[x] or row2[x] == row7[x] or row2[x] == row8[x] or row2[x] == row9[x]:
random.shuffle(row2)
while row3[x] == row1[x] or row3[x] == row2[x] or row3[x] == row4[x] or row3[x] == row5[x] or row3[x] == row6[x] or row3[x] == row7[x] or row3[x] == row8[x] or row3[x] == row9[x]:
random.shuffle(row3)
while row4[x] == row1[x] or row4[x] == row2[x] or row4[x] == row3[x] or row4[x] == row5[x] or row4[x] == row6[x] or row4[x] == row7[x] or row4[x] == row8[x] or row4[x] == row9[x]:
random.shuffle(row4)
while row5[x] == row1[x] or row5[x] == row2[x] or row5[x] == row3[x] or row5[x] == row4[x] or row5[x] == row6[x] or row5[x] == row7[x] or row5[x] == row8[x] or row5[x] == row9[x]:
random.shuffle(row5)
while row6[x] == row1[x] or row6[x] == row2[x] or row6[x] == row3[x] or row6[x] == row4[x] or row6[x] == row5[x] or row6[x] == row7[x] or row6[x] == row8[x] or row6[x] == row9[x]:
random.shuffle(row6)
while row7[x] == row1[x] or row7[x] == row2[x] or row7[x] == row3[x] or row7[x] == row4[x] or row7[x] == row5[x] or row7[x] == row6[x] or row7[x] == row8[x] or row7[x] == row9[x]:
random.shuffle(row7)
while row8[x] == row1[x] or row8[x] == row2[x] or row8[x] == row3[x] or row8[x] == row4[x] or row8[x] == row5[x] or row8[x] == row6[x] or row8[x] == row7[x] or row8[x] == row9[x]:
random.shuffle(row8)
while row9[x] == row1[x] or row9[x] == row2[x] or row9[x] == row3[x] or row9[x] == row4[x] or row9[x] == row5[x] or row9[x] == row6[x] or row9[x] == row7[x] or row9[x] == row8[x]:
random.shuffle(row9)
print(row1)
print(row2)
print(row3)
print(row4)
print(row5)
print(row6)
print(row7)
print(row8)
print(row9)
Is the problem in the for loop iteration or in the while loop logic? Thanks for any insights and assistance.