RANDOM_NUMBER

Intrinsic Subroutine: Returns one pseudorandom number or an array of such numbers.

Syntax

CALL RANDOM_NUMBER (harvest)

harvest
(Output) Must be of type real. It can be a scalar or an array variable. It is set to contain pseudorandom numbers from the uniform distribution within the range 0 <= x < 1.

The seed for the pseudorandom number generator used by RANDOM_NUMBER can be set or queried with RANDOM_SEED. If RANDOM_SEED is not used, the processor sets the seed for RANDOM_NUMBER to a processor-dependent value.

The RANDOM_NUMBER generator uses two separate congruential generators together to produce a period of approximately 10**18, and produces real pseudorandom results with a uniform distribution in (0,1). It accepts two integer seeds, the first of which is reduced to the range [1, 2147483562]. The second seed is reduced to the range [1, 2147483398]. This means that the generator effectively uses two 31-bit seeds.

For more information on the algorithm, see the following:

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: RANDOM_SEED, RANDOM, SEED, DRAND and DRANDM, IRAND and IRANDM, RAN, RAND and RANDOM

Examples

Consider the following:

REAL Y, Z (5, 5)
! Initialize Y with a pseudorandom number
CALL RANDOM_NUMBER (HARVEST = Y)
CALL RANDOM_NUMBER (Z)

Y and Z contain uniformly distributed random numbers.

The following shows another example:

 REAL x, array1 (5, 5)
 CALL RANDOM_SEED()
 CALL RANDOM_NUMBER(x)
 CALL RANDOM_NUMBER(array1)

Consider also the following:

  program testrand
    intrinsic random_seed, random_number
    integer size, seed(2), gseed(2), hiseed(2), zseed(2)
    real harvest(10)
    data seed /123456789, 987654321/
    data hiseed /-1, -1/
    data zseed /0, 0/
    call random_seed(SIZE=size)
    print *,"size ",size
    call random_seed(PUT=hiseed(1:size))
    call random_seed(GET=gseed(1:size))
    print *,"hiseed gseed", hiseed, gseed
    call random_seed(PUT=zseed(1:size))
    call random_seed(GET=gseed(1:size))
    print *,"zseed gseed ", zseed, gseed
    call random_seed(PUT=seed(1:size))
    call random_seed(GET=gseed(1:size))
    call random_number(HARVEST=harvest)
    print *, "seed gseed ", seed, gseed
    print *, "harvest"
    print *, harvest
    call random_seed(GET=gseed(1:size))
    print *,"gseed after harvest ", gseed
  end program testrand