RAND
Generates a scalar, rank-1, rank-2 or rank-3 array of random numbers.
Function Return Value
Scalar, rank-1, rank-2 or rank-3 array of random numbers. The output function value matches the input argument A in type, kind and rank. For complex arguments, the output values will be real and imaginary parts with random values of the same type, kind, and rank.  (Output)
Required Argument
A — The argument must be a scalar, rank-1, rank-2, or rank-3 array of type single, double, complex, or double complex. Used only to determine the type and rank of the output. (Input)
Optional Arguments, Packaged Options
Note: If any of the arrays s_rand_options(:), s_rand_options_once(:), d_rand_options(:), or d_rand_options_once(:) are allocated, they are passed as arguments to rand_gen using the keyword “iopt=”.
The option and derived type names are given in the following table:
Name of Unallocated Option Array to Use for Setting Options
Use
Derived Type
?_rand_options(:)
Use when setting options for calls hereafter.
?_options
?_rand_options_once(:)
Use when setting options for next call only.
?_options
FORTRAN 90 Interface
RAND(A)
Description
Generates a scalar, rank-1, rank-2 or rank-3 array of random numbers. Each component number is positive and strictly less than one in value.
This function uses rand_gen to obtain the number of values required by the argument. The values are then copied using the RESHAPE intrinsic
Example
 
use show_int
use rand_int
 
implicit none
 
! This is the equivalent of Example 1 for SHOW.
 
integer, parameter :: n=7, m=3
real(kind(1e0)) s_x(-1:n), s_m(m,n)
real(kind(1d0)) d_x(n), d_m(m,n)
complex(kind(1e0)) c_x(n), c_m(m,n)
complex(kind(1d0)) z_x(n),z_m(m,n)
integer i_x(n), i_m(m,n)
type (s_options) options(3)
 
! The data types printed are real(kind(1e0)), real(kind(1d0)),
! complex(kind(1e0)), complex(kind(1d0)), and INTEGER. Fill with random
! numbers and then print the contents, in each case with a label.
s_x=rand(s_x); s_m=rand(s_m)
d_x=rand(d_x); d_m=rand(d_m)
c_x=rand(c_x); c_m=rand(c_m)
z_x=rand(z_x); z_m=rand(z_m)
i_x=100*rand(s_x(1:n)); i_m=100*rand(s_m)
 
call show (s_x, 'Rank-1, REAL')
call show (s_m, 'Rank-2, REAL')
call show (d_x, 'Rank-1, DOUBLE')
call show (d_m, 'Rank-2, DOUBLE')
call show (c_x, 'Rank-1, COMPLEX')
call show (c_m, 'Rank-2, COMPLEX')
call show (z_x, 'Rank-1, DOUBLE COMPLEX')
call show (z_m, 'Rank-2, DOUBLE COMPLEX')
call show (i_x, 'Rank-1, INTEGER')
call show (i_m, 'Rank-2, INTEGER')
 
! Show 7 digits per number and -1 according to the
! natural or declared size of the array.
options(1)=show_significant_digits_is_7
options(2)=show_starting_index_is
options(3)= -1 ! The starting -1 value.
call show (s_x, &
'Rank-1, REAL with 7 digits, natural indexing', IOPT=options)
end
Published date: 03/19/2020
Last modified date: 03/19/2020