{"id":1177,"date":"2022-01-08T20:39:42","date_gmt":"2022-01-08T20:39:42","guid":{"rendered":"http:\/\/www.netexl.com\/blog\/?p=1177"},"modified":"2026-04-02T09:27:29","modified_gmt":"2026-04-02T09:27:29","slug":"create-a-matrix-with-random-numbers-with-no-repeat-vertically-or-horizontally","status":"publish","type":"post","link":"https:\/\/www.netexl.com\/blog\/create-a-matrix-with-random-numbers-with-no-repeat-vertically-or-horizontally\/","title":{"rendered":"Create a Matrix with Random Numbers with no Repeat Vertically or Horizontally"},"content":{"rendered":"\n<p>A code for reference picked from <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/51943125\/fill-a-matrix-with-random-number-with-no-repeat-vertically-or-horizontally\" target=\"_blank\">here<\/a> and converted to javascript<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let size = 9;\nlet board = &#91;];\n\nfor (let x = 0; x &lt; size; x++) {\n    board&#91;x] = createOrderedArray();\n    do {\n        shuffle(board&#91;x]);\n    } while (!compare2DArray(board&#91;x], board, 0, x));\n}\n\nconsole.log(board);\n\nfunction shuffle(arr) {\n    let count = arr.length;\n\n    for (let x = 0; x &lt; count; x++) {\n        swap(arr, x, Math.floor(Math.random() * count));\n    }\n}\n\nfunction createOrderedArray() {\n    let temp = &#91;];\n    let startVal = 1;\n    for (let i = 0; i &lt; size; i++) {\n        temp&#91;i] = startVal + i;\n    }\n    return temp;\n}\n\n\/\/Return TRUE if arrays are COMPLETELY different\nfunction compareArray(arr1, arr2) {\n    if (arr1.length !== arr2.length)\n        return false;\n\n    for (let x = 0; x &lt; arr1.length; x++) {\n        if (arr1&#91;x] == arr2&#91;x])\n            return false;\n    }\n    return true;\n}\n\n\/\/Return TRUE if array vs arrays is COMPLETELY different\nfunction compare2DArray(arr1, arr2, begin, end) {\n    for (let x = begin; x &lt; end; x++)\n        if (!compareArray(arr1, arr2&#91;x]))\n            return false;\n\n    return true;\n}\n\nfunction swap(arr, a, b) {\n    let temp = arr&#91;a];\n    arr&#91;a] = arr&#91;b];\n    arr&#91;b] = temp;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A code for reference picked from here and converted to javascript<\/p>\n","protected":false},"author":5,"featured_media":1539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,24],"tags":[],"class_list":["post-1177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","category-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/1177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/comments?post=1177"}],"version-history":[{"count":3,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/1177\/revisions"}],"predecessor-version":[{"id":1181,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/1177\/revisions\/1181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media\/1539"}],"wp:attachment":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media?parent=1177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/categories?post=1177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/tags?post=1177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}