Appendix 2 R code for randomizing in blocks
An R program ‘Blockrand’ is available on the CRAN mirror site (‘install’ and then ‘load’ Blockrand).
To quote the author Greg Snow ‘This function creates random assignments for clinical trials (or any experiment where the subjects come one at a time). The randomisation is done within blocks so that the balance between treatments stays close to equal throughout the trial.

The final block sizes will actually be the product of num.levels and block.sizes (e.g. if there are 2 levels and the default block sizes are used (1:4) then the actual block sizes will be randomly chosen from the set (2,4,6,8)).

Thus if t is the number of treatments and b is the specified  block size the program chooses block sizes at random in multiples of t up to bt.

Example

To generate a set of 100 allocations to treatment or control, with block sizes 2, 4, 6,or 8 we type.

b<-blockrand(100, num.levels=2, levels<-c('Treatment','Control'),,block.sizes=1:4)

To see the first 10 rows.

> b[1:10,]

   id block.id block.size treatment
1   1        1          4 Treatment
2   2        1          4   Control
3   3        1          4   Control
4   4        1          4 Treatment
5   5        2         12 Treatment
6   6        2         12 Treatment
7   7        2         12   Control
8   8        2         12 Treatment
9   9        2         12   Control
10 10        2         12   Control

To generate two strata, separately for males and females we type.

male <- blockrand(n=100, num.levels=2, levels<-c('Treatment','Control'),,block.sizes=1:4,id.prefix='M', ,stratum='Male') 

female <- blockrand(n=100, num.levels=2, levels<-c('Treatment','Control'),,block.sizes=1:4,id.prefix='F', stratum='Female')

We can join these together using blockout<-rbind(male,female) .

To see the first 10 rows.

> blockout[1:10,]
     id stratum block.id block.size treatment
1  M001    Male        1          4 Treatment
2  M002    Male        1          4 Treatment
3  M003    Male        1          4   Control
4  M004    Male        1          4   Control
5  M005    Male        2          4 Treatment
6  M006    Male        2          4 Treatment
7  M007    Male        2          4   Control
8  M008    Male        2          4   Control
9  M009    Male        3          4 Treatment
10 M010    Male        3          4   Control

One can see that all the male id numbers are prefixed by ‘M’.