Skip to main content

Randomizing Grids

Grids are common in assessments, and nodes often need to be placed on the grid randomly.

The method RandomDraws.FromGridWithoutReplacement() simplifies the process of randomly placing nodes on a grid. This method takes n, rows, and columns arguments. It returns an array of n objects to represent the randomly chosen grid cells. The grid cells have row and column properties (these are 0-based). The grid cell positions are unique.

This code returns an array of 2 grid cells, each with a unique row and column, from a grid with 4 rows and 4 columns:

const cells = RandomDraws.FromGridWithoutReplacement(2, 4, 4);

The cells object might look like this

[
{ row: 2, column: 2 },
{ row: 0, column: 2 },
]

Below, we create a 6 x 6 grid. We randomly place an X on 4 grid cells. The cell positions are also printed to the console.

Click the "Run" button multiple times to see how the positions of the X change.

Loading...
Explore:
  • Advanced: RandomDraws.FromGridWithoutReplacement() optionally takes a fourth argument, predicate, which is a function that takes row and column arguments and returns a boolean indicating if the cell meets a certain condition. This is useful if we need to apply a restriction to the grid randomization. Perhaps certain cells are not allowed to be chosen, or chosen cells must follow a certain pattern. For example, if need to restrict chosen cells to the diagonal, we could add a predicate function:
    const cells = RandomDraws.FromGridWithoutReplacement(4, 6, 6, (row, column) => {
        if (row === column) {
            return true;
        } else {
            return false;
        }
    });