RNPER
Generates a pseudorandom permutation.
Required Arguments
IPER — Vector of length K containing the random permutation of the integers from 1 to K. (Output)
Optional Arguments
K — Number of integers to be permuted. (Input)
Default: K = size (IPER,1).
FORTRAN 90 Interface
Generic: CALL RNPER (IPER [, …])
Specific: The specific interface name is S_RNPER.
FORTRAN 77 Interface
Single: CALL RNPER (K, IPER)
Description
Routine RNPER generates a pseudorandom permutation of the integers from 1 to K. It begins by filling a vector of length K with the consecutive integers 1 to K. Then, with M initially equal to K, a random index J between 1 and M (inclusive) is generated. The element of the vector with the index M and the element with index J swap places in the vector. M is then decremented by 1 and the process repeated until M = 1.
Comments
The routine RNSET can be used to initialize the seed of the random number generator. The routine RNOPT can be used to select the form of the generator.
Example
In this example, RNPER is called to produce a pseudorandom permutation of the integers from 1 to 10.
USE RNPER_INT
USE UMACH_INT
USE RNSET_INT
IMPLICIT NONE
INTEGER IPER(10), ISEED, NOUT
!
CALL UMACH (2, NOUT)
! Initialize seed of random number
! generator.
ISEED = 123457
CALL RNSET (ISEED)
CALL RNPER (IPER)
WRITE (NOUT,99999) IPER
99999 FORMAT (' Random permutation of the integers from 1 to 10', /, &
10I5)
END
Output
Random permutation of the integers from 1 to 10
5 9 2 8 1 6 4 7 3 10