this is the code for joel.neely suggestion
pass 3 for 3x3x3, 4 for 4x4x4, etc
Public Class Class1
Private aSquare(,,) As Integer
Private iSquare As Integer
Sub New(ByVal squareSize As Integer)
iSquare = squareSize
ReDim aSquare(iSquare - 1, iSquare - 1, CInt(iSquare ^ 2 - 1))
createSquare()
rndSquare()
End Sub
Private Sub createSquare()
Dim i As Integer, j As Integer, k As Integer
For i = 0 To aSquare.GetUpperBound(0)
For j = 0 To aSquare.GetUpperBound(1)
For k = 0 To aSquare.GetUpperBound(2)
If i = 0 And j = 0 Then
aSquare(i, j, k) = k + 1
ElseIf j = 0 And i > 0 Then
If (k + i) Mod (iSquare) = 0 Then
aSquare(i, j, k) = aSquare(i - 1, j, k) - (iSquare - 1)
Else
aSquare(i, j, k) = aSquare(i - 1, j, k) + 1
End If
Else
aSquare(i, j, k) = aSquare(i, j - 1, k) + iSquare
End If
If aSquare(i, j, k) > iSquare ^ 2 Then
aSquare(i, j, k) = aSquare(i, j, k) - CInt(iSquare ^ 2)
End If
Next
Next
Next
End Sub
Private Sub rndSquare()
Dim i As Integer
Randomize()
For i = 0 To CInt(iSquare ^ 2)
Select Case CInt(Rnd() * 3)
Case 0
rndBigCol()
Case 1
rndBigRow()
Case 2
rndLittleCol()
Case 3
rndlittleRow()
End Select
Next
End Sub
Private Sub rndBigCol()
Dim square As Integer
Dim rnd1, rnd2 As Integer
Dim i As Integer, j As Integer, k As Integer
Randomize()
For k = 0 To iSquare
Do
rnd1 = CInt(Rnd() * aSquare.GetUpperBound(1))
rnd2 = CInt(Rnd() * aSquare.GetUpperBound(1))
Loop Until rnd1 <> rnd2
For i = 0 To aSquare.GetUpperBound(0)
For j = 0 To aSquare.GetUpperBound(2)
square = aSquare(i, rnd1, j)
aSquare(i, rnd1, j) = aSquare(i, rnd2, j)
aSquare(i, rnd2, j) = square
Next
Next
Next
End Sub
Private Sub rndBigRow()
Dim square As Integer
Dim rnd1, rnd2 As Integer
Dim i As Integer, j As Integer, k As Integer
Randomize()
For k = 0 To iSquare
Do
rnd1 = CInt(Rnd() * aSquare.GetUpperBound(0))
rnd2 = CInt(Rnd() * aSquare.GetUpperBound(0))
Loop Until rnd1 <> rnd2
For i = 0 To aSquare.GetUpperBound(1)
For j = 0 To aSquare.GetUpperBound(2)
square = aSquare(rnd1, i, j)
aSquare(rnd1, i, j) = aSquare(rnd2, i, j)
aSquare(rnd2, i, j) = square
Next
Next
Next
End Sub
Private Sub rndLittleCol()
Dim square As Integer
Dim rnd1, rnd2, rnd3 As Integer
Dim i As Integer, k As Integer, l As Integer
Randomize()
For k = 0 To iSquare * 2
Do
rnd1 = CInt(Rnd() * aSquare.GetUpperBound(1))
rnd2 = CInt(Rnd() * (iSquare - 1))
rnd3 = CInt(Rnd() * (iSquare - 1))
Loop Until rnd2 <> rnd3
For i = 0 To aSquare.GetUpperBound(0)
For l = 0 To (iSquare - 1)
square = aSquare(i, rnd1, rnd2 + (l * iSquare))
aSquare(i, rnd1, rnd2 + (l * iSquare)) = aSquare(i, rnd1, rnd3 + (l * iSquare))
aSquare(i, rnd1, rnd3 + (l * iSquare)) = square
Next
Next
Next
End Sub
Private Sub rndlittleRow()
Dim square As Integer
Dim rnd1, rnd2, rnd3 As Integer
Dim j As Integer, k As Integer, l As Integer
Randomize()
For k = 0 To iSquare * 2
Do
rnd1 = CInt(Rnd() * aSquare.GetUpperBound(0))
rnd2 = CInt(Rnd() * (iSquare - 1))
rnd3 = CInt(Rnd() * (iSquare - 1))
Loop Until rnd2 <> rnd3
rnd2 *= iSquare
rnd3 *= iSquare
For j = 0 To aSquare.GetUpperBound(1)
For l = 0 To (iSquare - 1)
square = aSquare(rnd1, j, rnd2 + l)
aSquare(rnd1, j, rnd2 + l) = aSquare(rnd1, j, rnd3 + l)
aSquare(rnd1, j, rnd3 + l) = square
Next
Next
Next
End Sub
End Class