You cannot call the random_number
intrinsic in a pure procedure, but you could write a pure RNG that is slightly modified from a non-pure one, as discussed in Pure random number generators, and call that.
Alternatively, since a pure procedure can access a module variable but not change it, you could create a wrapper that calls random_number
, sets a module variable, and then calls the pure subroutine, as shown below.
module m
implicit none
real :: xran
contains
subroutine wrap_add(x)
real, intent(in out) :: x
call random_number(xran)
print*,"xran =",xran
call add(x)
end subroutine wrap_add
!
pure subroutine add(x)
real, intent(in out) :: x
x = x + xran
end subroutine add
end module m
!
program main
use m
implicit none
real :: x
x = 10.0
call wrap_add(x)
print*,"x =",x
call wrap_add(x)
print*,"x =",x
end program main
sample output:
xran = 0.349059999
x = 10.3490601
xran = 0.555532038
x = 10.9045925