Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 7/13/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

RANDOM_NUMBER

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

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.

The RANDOM_NUMBER generator does not produce subnormal numbers.

For more information on the algorithm, see the following:

  • Communications of the ACM vol 31 num 6 June 1988, titled: Efficient and Portable Combined Random Number Generators by Pierre L'ecuyer.

  • Springer-Verlag New York, N. Y. 2nd ed. 1987, titled: A Guide to Simulation by Bratley, P., Fox, B. L., and Schrage, L. E.

NOTE:

This routine is thread safe.

Example

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)

The following shows another example:


  program testrand
    intrinsic random_seed, random_number
    integer size 
    integer, allocatable :: seed(:), gseed(:), hiseed(:), zseed(:)
    real harvest(10)
    call random_seed(SIZE=size)
    print *,"size ",size
    allocate(seed(size),gseed(size),hiseed(size),zseed(size))
    hiseed = -1
    zseed = 0
    seed = 123456789
    seed(size) = 987654321
    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