Balanced Latin Square Generator

1234
1
2BCAD
3CDBA
4DACB
Download to .csv

What is it?

Wikipedia defines a latin square as "an n × n array filled with n different symbols, each occurring exactly once in each row and exactly once in each column.". Latin squares are useful to reduce order-effects when designing experiments with multiple conditions. For example, in an experiment comparing a technique A vs B vs C, if all participants test A first, then B, then C, we might observe poor results for C not because C is worse than A or B, but because of participants' fatigue. Instead, if we order conditions based on a randomly generated latin square, we could obtain the following order.

Participant # 1st 2nd 3rd
1 A B C
2 C A B
3 B C A

By ordering conditions using a latin square, we reduce the order-effect since conditions appear the same number of times in each position. However, there is still an issue caused by the repeated sequences: B is often preceded by A, C by B, and A by C. This could cause a carry-over effect.

Balanced Latin Squares (the ones generated above) are special cases of latin square that remove immediate carry-over effects: A condition will precede another exactly once (or twice, if the number of conditions is odd). This page is a simple generator of balanced latin square. The generator uses the method proposed and mathematically proved by James V. Bradley in "Complete Counterbalancing of Immediate Sequential Effects in a Latin Square Design".


Source code

The code of this generator is open source. Feel free to copy the following function to generate balanced latin squares directly from your code.

Javascript