26 public :: random_uniform
27 public :: random_normal
29 interface random_uniform
31 module procedure random_uniform_2d
32 module procedure random_uniform_3d
35 interface random_normal
36 module procedure random_normal_1d
37 module procedure random_normal_2d
38 module procedure random_normal_3d
39 end interface random_normal
49 private :: random_reset
55 logical,
private :: RANDOM_FIX = .false.
57 integer,
private,
allocatable :: RANDOM_seedvar(:)
68 namelist / param_random / &
71 integer :: nseeds, ierr
75 log_info(
"RANDOM_setup",*)
'Setup'
81 log_info(
"RANDOM_setup",*)
'Not found namelist. Default used.'
82 elseif( ierr > 0 )
then
83 log_error(
"RANDOM_setup",*)
'Not appropriate names in namelist PARAM_RANDOM. Check!'
89 call random_seed(size=nseeds)
91 allocate( random_seedvar(nseeds))
94 log_info(
"RANDOM_setup",*)
'Array size for random seed:', nseeds
95 if ( random_fix )
then
96 log_info(
"RANDOM_setup",*)
'random seed is fixed.'
106 subroutine random_reset
115 if ( random_fix )
then
127 call date_and_time(values=time1)
132 + ( time1(1) - 1970 ) * 32140800 &
133 + time1(2) * 2678400 &
141 call random_seed(put=random_seedvar)
144 end subroutine random_reset
150 real(RP),
intent(out) :: var(:)
153 call random_number(var)
160 subroutine random_uniform_2d( var )
162 real(RP),
intent(out) :: var(:,:)
165 call random_number(var)
168 end subroutine random_uniform_2d
172 subroutine random_uniform_3d( var )
174 real(RP),
intent(out) :: var(:,:,:)
177 call random_number(var)
180 end subroutine random_uniform_3d
184 subroutine random_normal_1d( var )
186 real(RP),
intent(out) :: var(:)
190 call get_normal( n, var(:) )
193 end subroutine random_normal_1d
196 subroutine random_normal_2d( var )
198 real(RP),
intent(out) :: var(:,:)
202 call get_normal( n, var(:,:) )
205 end subroutine random_normal_2d
208 subroutine random_normal_3d( var )
210 real(RP),
intent(out) :: var(:,:,:)
214 call get_normal( n, var(:,:,:) )
217 end subroutine random_normal_3d
221 subroutine get_normal( n, var )
225 integer,
intent(in) :: n
226 real(RP),
intent(out) :: var(n)
234 call random_number(rnd)
239 fact = sqrt(-2.0_rp * log( 1.0_rp - rnd(i*2-1) ) )
240 theta = 2.0_rp * pi * rnd(i*2)
241 var(i*2-1) = fact * cos(theta)
242 var(i*2 ) = fact * sin(theta)
244 if ( mod(n,2) == 1 )
then
245 fact = sqrt(-2.0_rp * log( 1.0_rp - rnd(n) ) )
246 theta = 2.0_rp * pi * rnd(n+1)
247 var(n) = fact * cos(theta)
251 end subroutine get_normal