I am trying to make a 5×5 table that will have the numbers 1-5 random put in the rows/columns. But they need to have all different numbers for each row and column.
ex:
12345
54123
41532
35214
23451
The code I currently have is very long so I will give a pastebin link to it. http://pastebin.com/ex1bcLxh
Any and all help will be appreciated.
Here’s what I came up with off the top of my head – I haven’t bothered with optimisation because for the grid size of 5×5 mentioned in the question it feels instantaneous. (Testing in IE7 even 7×7 grids only take a couple of seconds. 8×8 is noticeably slower, sometimes slow enough to trigger the long-running script error.)
In case it’s not obvious, the basic idea is to start off with a row [5,4,3,2,1]; shuffle the row and check if it can be added; repeat until done.
(I thought about starting by generating all potential rows and then randomly choosing from them, but that seemed like too much hassle given that I’m not very good with permutation algorithms – just doing a random shuffle each time seemed a lot less trouble.)