I am looking for an algorithm that does as follows:

Given a 2D grid of X,Y and a number of tiles T, places a tile in each cell of the grid such that the surrounding tiles are not the same.

This should also ideally be capable of providing a random seed to randomize the output.

I understand that for not all values of X, Y and T this is possible, but is there an existing algorithm that will do what I'm looking for? I'm currently using a naive hand-rolled implementation that doesn't always provide the best results.

Is something like a constraint solver what I need?

  • What do you mean by "best results?" How large are these grids? Commented Oct 16, 2014 at 17:10
  • "Not best results" in that sometimes adjacent tiles appear. The grid can be any size, i.e. user-defined.
    – Groky
    Commented Oct 16, 2014 at 20:09
  • Sounds like a Coloring problem. There is a lot of research and options as to how to solve them.
    – Sign
    Commented Oct 17, 2014 at 19:55
  • 1
    I don't think I understand the question. If you want a grid to be tiled with non-adjacent tiles, just use a checkerboard pattern. What am I not seeing? Commented Oct 17, 2014 at 22:17
  • The pattern needs to be random. Think tiling a surface in a 3D scene with different tiles in order to hide repeating patterns. Along those lines.
    – Groky
    Commented Oct 20, 2014 at 0:27

1 Answer 1


Randomly distribute the most numerous tile so that there are no adjacencies. Then randomly distribute the next most numerous tile, and so on. That should work well unless the two most numerous tiles cover almost the entire rectangle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.