Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

RAND, RANDOM

Portability Functions: Return real random numbers in the range 0.0 to 1.0, not including the end points.

Module

USE IFPORT

result = RAND ([ iflag ])

result = RANDOM (iflag)

iflag

(Input) INTEGER(4). Optional for RAND. Controls the way the random number is selected.

Results

The result type is REAL(4). RAND and RANDOM return random numbers in the range 0.0 to 1.0, not including the end points.

Value of iflag

Selection process

1

The generator is restarted, and the first random value is selected.

0

The next random number in the sequence is selected.

Otherwise

The generator is reseeded using iflag, restarted, and the first random value is selected.

When RAND is called without an argument, the following applies:

  • The value of iflag is assumed to be 0.

  • You must specify USE IFPORT.

There is no difference between RAND and RANDOM. Both functions are included to ensure portability of existing code that references one or both of them.

The intrinsic subroutines RANDOM_INIT, RANDOM_NUMBER, and RANDOM_SEED provide the same functionality, and they are the recommended functions to use when writing programs to generate random numbers.

You can use SRAND to restart the pseudorandom number generator used by RAND.

NOTE:

RANDOM is available as a function or subroutine.

Example

The following example shows how to use both the RANDOM function and the RANDOM subroutine:

use ifport
real(4) ranval
call seed(1995) ! initialize
call random(ranval) ! get next random number
print *,ranval

ranval = random(1) ! initialize
ranval = random(0) ! get next random number

print *,ranval
end