Can You Help Me Find an Algorithm to Fill a Grid Without Repeating Places?

  • Thread starter Thread starter calluscus
  • Start date Start date
  • Tags Tags
    Algorithm Grid
AI Thread Summary
An algorithm is sought to fill a grid without repeating places, with interest in pseudorandom number lists. A proposed solution involves filling the grid with integers from 1 to r * c, then using a swapping method similar to Knuth's shuffling algorithm in two dimensions. The provided C code outlines the process of initializing the grid and performing random swaps to achieve a filled grid without duplicates. The approach ensures that each cell is populated uniquely while maintaining randomness. This method is suitable for small grids and can be adapted for larger ones with modifications.
Mathematics news on Phys.org
calluscus said:
Hi:

I'm looking for an algorithm to fill a grid without repeating places.
I have looking for Pseudorandom number lists (http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators#gsl_rng_minstd) but i do not get it clearly...

Any help ? :)

Well, what exactly are you trying to accomplish? If the grid is small enough, you could fill in the grid with integers, 1 ... r * c, and then for each cell, swap its value with another, random cell, that is to the right or below the current cell. This is similar to Knuth's shuffling algorithm except in two dimensions. C code follows.

Code:
#include <stdlib.h>

#define X (80)
#define Y (24)

int main()
{
  int x, y; /* current grid position */
  int i, j; /* x, y for random grid position with which to swap*/
  int k;    /* auxiliary variable to fill out the initial sorted grid */
  int t;    /* auxiliary variable for swapping */
  int grid[X][Y];

  srand(time(NULL)); /* seed the random number generator */
  k = 0;

  /* fill in the grid with (1, 2, ..., x*y) */
  for (y = 0; y < Y; ++y)
    for (x = 0; x < X; ++x)
      grid[x][y] = ++k;

  for (y = 0; y < Y; ++y)
    for (x = 0; x < X; ++x)
    {
      /* select a random row between this one and the last one */
      j = rand() % (Y - y) + y;

      if (j == y)
      {
        /* if it's on the current row, select a column to our right */
        i = rand() % (X - x) + x;
      }
      else
      {
        /* if it's on a row below, select _any_ column */
        i = rand() % X;
      }

      /* swap */
      t = grid[i][j];
      grid[i][j] = grid[x][y];
      grid[x][y] = t;
    }

  /* do stuff with the grid here */

  return 0;
}

(note: I have not even so much as compiled the above code, but hopefully you can follow what I'm trying to do)
 
Last edited:
ty very much
 
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Suppose ,instead of the usual x,y coordinate system with an I basis vector along the x -axis and a corresponding j basis vector along the y-axis we instead have a different pair of basis vectors ,call them e and f along their respective axes. I have seen that this is an important subject in maths My question is what physical applications does such a model apply to? I am asking here because I have devoted quite a lot of time in the past to understanding convectors and the dual...
Back
Top