Balanced Latin Square Generator

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 because of participants' fatigue and not because C is worse than A or B. 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 time as first, second, etc. However, 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 Squares which 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 that James V. Bradley proposed and mathematically proved in "Complete Counterbalancing of Immediate Sequential Effects in a Latin Square Design".

Source code

You can also generate balanced latin squares directly from your code by copying the following function.