Byers, J.A. 1996. Random selection algorithms for spatial
and temporal sampling. Computers in Biology and
excerpt from page 47:
A Latin square of 5 treatment numbers on a side has 5 rows and
5 columns of numbers (1 to 5) in which each number occurs only once
in each row and column and all treatment numbers are represented in
each row and column, yielding 25 cells with numbers. Algorithms for
rapid construction of Latin squares of any size are given in
Program 2 of Byers . This method uses a randomized column and
row array (each equal to the number of treatments) whose
intersection sums give the value for each cell. However, if the sum
is larger than the number of different treatments then this number
is subtracted from the sum [10, 15, 16]. Although fast, this method
is not capable of generating all possible Latin squares since only
N!(N-1)! squares can be obtained, or 2880 for a 5 x 5 square .
In fact, for 5 x 5 Latin squares there are 161,280 different
possible permutations [4, 8].
The program (Fig. 5A) used in a 486-type computer can make
Latin squares of up to about 20 cells on a side in a few seconds,
and in theory any of the millions of possible squares can be
obtained. The algorithm picks at random a treatment number for a
cell in a row from a temporary row array such that this number is
no longer available for selection, but first cells in the same
column from previously generated rows are checked for redundancy,
if such is found then the number is kept in the temporary array and
another number is tried until no redundancies are found, whereupon
the program moves to the next cell in the row, or a new row. It is
possible that no number remaining in the temporary array can
satisfy the conditions so after 50 tries at random, the entire row
is erased and another random sequence is tried until satisfying the
conditions of a Latin square.
10 CLS : DEFINT A-Z: COLOR 15: PRINT "ANY POSSIBLE LATIN SQUARE"
20 COLOR 11: INPUT "ENTER NUMBER OF TREATMENTS"; N
30 INPUT "ENTER NUMBER FOR RANDOM SEED"; RN: RN = RND(-RN)
40 COLOR 15: DIM C(N): DIM A(N, N)
50 FOR R = 1 TO N
60 FOR C = 1 TO N: C(C) = C: NEXT C: J = N
70 FOR C = 1 TO N: I = 0
80 X = INT(RND * J + 1)
90 REM CHECK COLUMN IF OK (ROW IS INHERENTLY OK)
100 FOR H = 1 TO R - 1
110 IF I > 50 THEN 60: REM ROW NO GOOD
120 IF A(H, C) = C(X) THEN I = I + 1: GOTO 80: REM COLUMN NO GOOD
130 NEXT H
140 A(R, C) = C(X): J = J - 1
150 FOR K = X TO J: C(K) = C(K + 1): NEXT K: NEXT C
160 FOR CL = 1 TO N: PRINT USING "###"; A(R, CL); : NEXT: PRINT : NEXT
ANY POSSIBLE LATIN SQUARE
ENTER NUMBER OF TREATMENTS? 8
ENTER NUMBER FOR RANDOM SEED? 1
1 2 3 4 7 6 5 8
6 4 1 8 3 7 2 5
5 8 6 3 1 2 7 4
7 5 8 1 2 4 3 6
3 6 5 2 4 1 8 7
4 3 7 6 5 8 1 2
8 7 2 5 6 3 4 1
2 1 4 7 8 5 6 3
Fig. 5. (A) Listing of BASIC program used to randomly select any possible
Latin square (each treatment represented once in each column and row).
(B) Printed output of program on computer screen of an IBM PC or
compatible computer (output may differ due to BASIC version used).
One enters the number of treatments (8) and a random seed. The
progress of the calculations are shown row by row, the first rows are of
course generated rapidly, while for larger squares of 15 or more cells
per side, the last rows take increasingly longer times to find solutions.
Output of the program is shown in Fig. 5B. One could imagine a test in
which the unequal effects of area and time on attraction of bark beetles
to 8 chemical baits are evened by testing each bait once for a day in
each of 8 areas (columns) for a period of 8 days (rows).
4. B. E. Cooper, Statistics for Experimentalists. Pergamon Press, London
8. W. T. Federer, Experimental Design. MacMillan Co., New York (1955).
9. O. Kempthorne, The Design and Analysis of Experiments. Wiley, New York
10. J. A. Byers, Basic algorithms for random sampling and treatment
randomization. Comput. Biol. Med. 21, 69 (1991).
15. W. L. Stevens, The completely orthogonalised square. Ann. Eug. 9, 82
16. R. C. Bose, On the application of Galois fields to the problem of the
construction of Hyper-Graeco-Latin squares. Sankhya 3, 323 (1938).
Other related references:
Byers, J.A. 1991. BASIC algorithms for random sampling and treatment
randomization. Computers in Biology and Medicine 21:69-77.
Byers, J.A. 1993. Randomization algorithms in BASIC for experimental
design. Computers in Biology and Medicine 23:167-176.