Tasteful tiling with Excel

If you arrived here looking for information on shuffling lists, lottery numbers and choosing random numbers that don't repeat, you may be interested in this article.

Someone who fits kitchens and bathrooms for a living asked for help with tiling: "Clients often want a random effect using different colours. You wouldn't believe how long we spend trying to get arrangements that look right. It's very wasteful of time, and then the client comes home and says she can see a space invader near the cooker."

He had tried to produce random patterns with Excel so that the proposed tiling scheme could be approved by the client. Unfortunately, his efforts with the RAND function did not look very random due to clusters of tiles of the same colour:

Image showing pattern produced by RAND function

I was asked whether these clumps are due to a bug in RAND, and if there is a way to randomly distribute a fixed stock of tiles of different colours without these shapes turning up.

Yes there is, but not by filling cells with RAND functions. RAND generates a number without reference to other cells. You can bias the result towards or away from certain numbers but this isn't the best solution to the problem.

I don't want what I asked for

There are subtle factors at work in random tile arrangements. When clients ask for a random pattern, they may not be asking for what they have in mind.

Download the file Tiles.xls from here. Open it and click the 'Truly random' button a few times. Watch the patterns. The chances are that parts of them will be as unordered as anyone could wish, but most of what you see has a degree of clumpiness – areas of the same colour that form shapes the brain picks out as non-random.

And yet the VBA program behind this button is producing genuinely random patterns. The way it works is to lay all available tiles in a line, thoroughly shuffle them into random order, then cut the line into strips that are laid out in rows to cover the area.

The fewer colours involved the more likely it is that a tile will fall next to another of the same colour purely by chance. The resulting clusters do not represent a bug. If there were no clumping in most of the patterns, the selection process would not be truly random. How often do the lottery numbers come out evenly spaced? Very rarely. There is normally a degree of bunching and this is exactly how a truly random system behaves.

Clumping of tile colours may be pleasing to some. Others will say it is not what they want. If you press the button enough times, the random shuffling will eventually generate every possible arrangement of tiles, including relatively un-clustered ones like this:

Image showing highly chaotic pattern with few clumps

How many times? Lots and lots. The number of unique patterns you can make with four colours in a 12 x 10 grid is comparable to the number of atoms in our galaxy. That's a lot of mouse pressing and not a few lifetimes!

If you could examine all those possible patterns, in addition to all the random-looking ones, you would see every possible image that could be represented with that number of pixels in those colours. Every possible face, every possible fragment of writing in any possible language invented or not, next week's lottery numbers... anything. But since you'd have no way of picking out the relative few that appear to make sense, and no way of distinguishing true information from false, it would be of no use even if you had the time.

Any pattern generated in this way is random in the sense of being a random selection from all possible patterns. A few look very regular and planned. Some look totally chaotic. Between the extremes are a massive number that have a degree of clumpiness. These intermediate types are in the majority so they come up most often.

Cluster's last stand

What some clients really want is a one of the sub-set of patterns that appear chaotic. Is there a way to favour generation of these patterns? Press the 'Scatter' button and you will see there is.

The macro behind this button begins by making a list of all the empty positions to be tiled. The list is shuffled using the same method as before and the area is tiled in the random order so produced.

By itself this would not reduce clustering. It is just a different way of generating the same truly random patterns as the first button, warts and all.

The twist is that at each position the program counts the colours of nearby tiles. A random choice is made from among the least popular colours. If the stock of the chosen colour has been used up then an alternative unpopular choice is made.

Image illustrating macro algorithm

Here, the program would see that the position about to be tiled has two orange neighbours, one yellow and one green. Its first choice would be a random selection from the locally unrepresented blue and purple colours. If stocks of both were gone, it would make a random choice between the next most unpopular colours, yellow and green. Should they be used up also, orange would then be selected.

The effect of this local survey is to increase colour diversity around a tile, thus decreasing clumping. The brain's forte is identifying patterns so even with this improved algorithm you will see artefacts, though fewer than before and not so large. Increasing the number of colours helps. Six colours seems to be the minimum required to produce consistently good results with the Scatter algorithm.

Another variable you can play with is the size of the area examined around each tile. This is the value marked Scope, and note that it is not used by the first, truly random algorithm. A scope of 1 means the program examines immediately adjacent tiles – a 3 x 3 block. Upping the value to 2 extends the reach by another tile in every direction, giving a block size of 5 x 5, and so on.

Pushing the scope value upwards without also increasing the number of available colours increases local clustering. This is because examining a larger number of tiles reduces the possibility of making a unique choice.

The program run by the 'Scatter+' button is a high-strength version of Scatter. It is hard-wired with a scope of 2 and takes account of a tile's relative position as well as its colour.

The influence of the four immediately adjacent horizontal and vertical tiles is magnified by a weighting system. It is much less likely that a tile of the same colour as one in those positions will be selected. Push this improved program into a corner by choosing a low number of colours and it tends to favour diagonal lines. On five colours Scatter+ generates good patterns where Scatter exhibits clumping.

Image showing output in Excel

You may find a pattern that's almost right except for a little tweaking. The worksheet has provision for this, allowing manual swapping of tiles. Select one tile, [Ctrl]-click the other and press the Swap button.

 Didn't find what you want? Search again… Google
All iansharpe.com site content and software © Ian Sharpe July 2008. Nothing may be reproduced without permission. See the full copyright statement. Use of iansharpe.com programs, information and advertisers signifies acceptance of the disclaimer. A privacy statement may be read here. Small donations towards my running costs are welcome – please click here.
Valid XHTML 1.0! Valid CSS!

Disclaimer