SUBROUTINE random_order(order, n)
! Generate a random ordering of the integers 1 ... n.
INTEGER, INTENT(IN) :: n
INTEGER, INTENT(OUT) :: order(n)
! Local variables
INTEGER :: i, j, k
REAL :: wk
DO i = 1, n
order(i) = i
END DO
! Starting at the end, swap the current last indicator with one
! randomly chosen from those preceeding it.
DO i = n, 2, -1
CALL RANDOM_NUMBER(wk)
j = 1 + int(i * wk)
IF (j < i) THEN
k = order(i)
order(i) = order(j)
order(j) = k
END IF
END DO
RETURN
END SUBROUTINE random_order