29 public :: random_uniform
30 public :: random_normal
34 interface random_uniform
36 module procedure random_uniform_2d
37 module procedure random_uniform_3d
40 interface random_normal
41 module procedure random_normal_1d
42 module procedure random_normal_2d
43 module procedure random_normal_3d
44 end interface random_normal
54 private :: random_reset
60 logical,
private :: RANDOM_FIX = .false.
62 integer,
private,
allocatable :: RANDOM_seedvar(:)
73 namelist / param_random / &
76 integer :: nseeds, ierr
80 log_info(
"RANDOM_setup",*)
'Setup'
86 log_info(
"RANDOM_setup",*)
'Not found namelist. Default used.'
87 elseif( ierr > 0 )
then
88 log_error(
"RANDOM_setup",*)
'Not appropriate names in namelist PARAM_RANDOM. Check!'
94 call random_seed(size=nseeds)
96 allocate( random_seedvar(nseeds))
99 log_info(
"RANDOM_setup",*)
'Array size for random seed:', nseeds
100 if ( random_fix )
then
101 log_info(
"RANDOM_setup",*)
'random seed is fixed.'
111 subroutine random_reset
120 if ( random_fix )
then
132 call date_and_time(values=time1)
137 + ( time1(1) - 1970 ) * 32140800 &
138 + time1(2) * 2678400 &
146 call random_seed(put=random_seedvar)
149 end subroutine random_reset
155 real(RP),
intent(out) :: var(:)
158 call random_number(var)
166 subroutine random_uniform_2d( var )
168 real(RP),
intent(out) :: var(:,:)
171 call random_number(var)
175 end subroutine random_uniform_2d
179 subroutine random_uniform_3d( var )
181 real(RP),
intent(out) :: var(:,:,:)
184 call random_number(var)
188 end subroutine random_uniform_3d
192 subroutine random_normal_1d( var )
194 real(RP),
intent(out) :: var(:)
198 call get_normal( n, var(:) )
201 end subroutine random_normal_1d
204 subroutine random_normal_2d( var )
206 real(RP),
intent(out) :: var(:,:)
210 call get_normal( n, var(:,:) )
213 end subroutine random_normal_2d
216 subroutine random_normal_3d( var )
218 real(RP),
intent(out) :: var(:,:,:)
222 call get_normal( n, var(:,:,:) )
225 end subroutine random_normal_3d
234 deallocate( random_seedvar )
241 integer,
intent(in) :: num
242 integer,
intent(out) :: a(num)
243 integer :: i, randpos, tmp
251 call random_number(r)
252 randpos = int(r * i) + 1
263 subroutine get_normal( n, var )
267 integer,
intent(in) :: n
268 real(
rp),
intent(out) :: var(n)
276 call random_number(rnd)
281 fact = sqrt(-2.0_rp * log( 1.0_rp - rnd(i*2-1) ) )
282 theta = 2.0_rp * pi * rnd(i*2)
283 var(i*2-1) = fact * cos(theta)
284 var(i*2 ) = fact * sin(theta)
286 if ( mod(n,2) == 1 )
then
287 fact = sqrt(-2.0_rp * log( 1.0_rp - rnd(n) ) )
288 theta = 2.0_rp * pi * rnd(n+1)
289 var(n) = fact * cos(theta)
295 end subroutine get_normal