Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:55:40+00:00 2026-05-27T06:55:40+00:00

I am trying to make a 5×5 table that will have the numbers 1-5

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:55:41+00:00Added an answer on May 27, 2026 at 6:55 am

    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.)

    function createGrid(size) {
       var grid = [],
           row = [],
           x,y,t;
    
       for (x=size; x>0; x--)
          row.push(x);
    
       addRows:
       while (grid.length < size) {
          t = row.slice();
          row = [];
          while (t.length > 0)
             row.push(t.splice(Math.floor(Math.random() * t.length), 1)[0]);
    
          for (y=0; y<grid.length; y++)
             for (x=0; x<size; x++)
                if (row[x] === grid[y][x])
                   continue addRows;
    
          grid.push(row);
       }
    
       return grid;   
    }
    
    var test = createGrid(5);
    alert(test.join("\n"));
    

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make simple minesweeper game in python, but have one problem. I have
Right now I'm trying make some browser based game. But I have little questions.
Trying to make a make generic select control that I can dynamically add elements
Trying to make a custom :confirm message for a rails form that returns data
trying to make a page which will recursively call a function until a limit
I'm trying to make a route parameter match an alphanumeric value that's 3 to
im trying make one replace in string from a array but this dont work
I'm trying make a static text which if too long for the sizer, will
I'm trying make one treeView with infinite subgroups. I can add my groups but
I'm trying to make a change to an old appengine application, but now after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.