Web Listing 3: Code Producing Graphical Depiction of a Random Solution WITH Nums(n) AS ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 ), Strings(string, strlen) AS ( SELECT CAST(n AS VARCHAR(8)), 1 FROM Nums UNION ALL SELECT CAST(PRV.string + CAST(NXT.n AS VARCHAR(1)) AS VARCHAR(8)), PRV.strlen + 1 FROM Strings AS PRV JOIN Nums AS NXT ON PRV.strlen < 8 AND PRV.string NOT LIKE '%' + CAST(NXT.N AS VARCHAR(1)) + '%' AND NOT EXISTS (SELECT * FROM Nums AS D WHERE D.n <= PRV.strlen AND PRV.strlen + 1 - D.n = ABS(NXT.n - CAST(SUBSTRING(PRV.string, D.n, 1) AS INT))) ), Solutions AS ( SELECT string FROM Strings WHERE strlen = 8 ), RandomSolution AS ( SELECT TOP (1) string FROM Solutions ORDER BY CHECKSUM(NEWID()) ), UnpivotedString AS ( SELECT CAST(SUBSTRING(string, C.n, 1) AS INT) AS row, C.n AS col FROM RandomSolution JOIN Nums AS C ON C.n <= 8 ) SELECT row, [1] AS a, [2] AS b, [3] AS c, [4] AS d, [5] AS e, [6] AS f, [7] AS g, [8] AS h FROM UnpivotedString PIVOT(COUNT(col) FOR col IN([1],[2],[3],[4],[5],[6],[7],[8])) AS P ORDER BY row;