103 integer,
parameter:: n=624
104 integer,
parameter:: m=397
105 integer,
parameter:: mata=-1727483681
106 integer,
parameter:: umask=-2147483648
107 integer,
parameter:: lmask =2147483647
108 integer,
parameter:: tmaskb=-1658038656
109 integer,
parameter:: tmaskc=-272236544
110 integer,
parameter:: mag01(0:1)=(/0,mata/)
111 integer,
parameter:: iseed=4357
112 integer,
parameter:: nrest=n+6
122 type(random_stat),
save:: sstat
124 interface random_setseed
125 module procedure random_setseed_s
126 module procedure random_setseed_t
128 interface random_number
129 module procedure random_number_i
130 module procedure random_number_s
131 module procedure random_number_t
133 interface random_gauss
134 module procedure random_gauss_i
135 module procedure random_gauss_s
136 module procedure random_gauss_t
138 interface random_index
139 module procedure random_index_i
140 module procedure random_index_s
141 module procedure random_index_t
152 integer,
intent(out),
optional:: size
153 integer,
intent(in),
optional:: put(nrest)
154 integer,
intent(out),
optional:: get(nrest)
155 type(random_stat),
intent(inout),
optional:: stat
156 if(
present(size))
then
160 elseif(
present(put))
then
163 if(
present(stat))
then
167 stat%gset=transfer(put(n+3:nrest),stat%gset)
168 if(stat%mti.lt.0.or.stat%mti.gt.n.or.any(stat%mt.eq.0).or.
169 & stat%iset.lt.0.or.stat%iset.gt.1)
then
170 call random_setseed_t(iseed,stat)
177 sstat%gset=transfer(put(n+3:nrest),sstat%gset)
178 if(sstat%mti.lt.0.or.sstat%mti.gt.n.or.any(sstat%mt.eq.0)
179 & .or.sstat%iset.lt.0.or.sstat%iset.gt.1)
then
180 call random_setseed_t(iseed,sstat)
184 elseif(
present(get))
then
185 if(
present(stat))
then
186 if(stat%mti.eq.n+1)
call random_setseed_t(iseed,stat)
190 get(n+3:nrest)=transfer(stat%gset,get,nrest-(n+3)+1)
192 if(sstat%mti.eq.n+1)
call random_setseed_t(iseed,sstat)
196 get(n+3:nrest)=transfer(sstat%gset,get,nrest-(n+3)+1)
199 if(
present(stat))
then
200 call random_setseed_t(iseed,stat)
202 call random_setseed_t(iseed,sstat)
208 subroutine random_setseed_s(inseed)
210 integer,
intent(in):: inseed
211 call random_setseed_t(inseed,sstat)
216 subroutine random_setseed_t(inseed,stat)
218 integer,
intent(in):: inseed
219 type(random_stat),
intent(out):: stat
224 stat%mt(0)=iand(ii,-1)
226 stat%mt(mti)=iand(69069*stat%mt(mti-1),-1)
237 if(sstat%mti.eq.n+1)
call random_setseed_t(iseed,sstat)
238 call random_number_t(h,sstat)
244 subroutine random_number_i(harvest,inseed)
246 real,
intent(out):: harvest(:)
247 integer,
intent(in):: inseed
248 type(random_stat) stat
249 call random_setseed_t(inseed,stat)
250 call random_number_t(harvest,stat)
254 subroutine random_number_s(harvest)
256 real,
intent(out):: harvest(:)
257 if(sstat%mti.eq.n+1)
call random_setseed_t(iseed,sstat)
258 call random_number_t(harvest,sstat)
263 subroutine random_number_t(harvest,stat)
265 real,
intent(out):: harvest(:)
266 type(random_stat),
intent(inout):: stat
268 integer tshftu,tshfts,tshftt,tshftl
269 tshftu(y)=ishft(y,-11)
271 tshftt(y)=ishft(y,15)
272 tshftl(y)=ishft(y,-18)
274 if(stat%mti.ge.n)
then
276 y=ior(iand(stat%mt(kk),umask),iand(stat%mt(kk+1),lmask))
277 stat%mt(kk)=ieor(ieor(stat%mt(kk+m),ishft(y,-1)),
281 y=ior(iand(stat%mt(kk),umask),iand(stat%mt(kk+1),lmask))
282 stat%mt(kk)=ieor(ieor(stat%mt(kk+(m-n)),ishft(y,-1)),
285 y=ior(iand(stat%mt(n-1),umask),iand(stat%mt(0),lmask))
286 stat%mt(n-1)=ieor(ieor(stat%mt(m-1),ishft(y,-1)),
292 y=ieor(y,iand(tshfts(y),tmaskb))
293 y=ieor(y,iand(tshftt(y),tmaskc))
296 harvest(j)=(real(y)+2.0**32)/(2.0**32-1.0)
298 harvest(j)=real(y)/(2.0**32-1.0)
309 if(sstat%mti.eq.n+1)
call random_setseed_t(iseed,sstat)
310 call random_gauss_t(h,sstat)
316 subroutine random_gauss_i(harvest,inseed)
318 real,
intent(out):: harvest(:)
319 integer,
intent(in):: inseed
320 type(random_stat) stat
321 call random_setseed_t(inseed,stat)
322 call random_gauss_t(harvest,stat)
326 subroutine random_gauss_s(harvest)
328 real,
intent(out):: harvest(:)
329 if(sstat%mti.eq.n+1)
call random_setseed_t(iseed,sstat)
330 call random_gauss_t(harvest,sstat)
335 subroutine random_gauss_t(harvest,stat)
337 real,
intent(out):: harvest(:)
338 type(random_stat),
intent(inout):: stat
344 if(stat%iset.eq.1)
then
351 call random_number_t(harvest(mx+1:my),stat)
353 call rgauss(harvest(j+1),harvest(j+2),r,g1,g2)
364 call random_number_t(r2,stat)
365 call rgauss(r2(1),r2(2),r,g1,g2)
379 subroutine rgauss(r1,r2,r,g1,g2)
380 real,
intent(in):: r1,r2
381 real,
intent(out):: r,g1,g2
387 fac=sqrt(-2.*log(r)/r)
398 integer,
intent(in):: imax
401 if(sstat%mti.eq.n+1)
call random_setseed_t(iseed,sstat)
402 call random_index_t(imax,ih,sstat)
409 subroutine random_index_i(imax,iharvest,inseed)
411 integer,
intent(in):: imax
412 integer,
intent(out):: iharvest(:)
413 integer,
intent(in):: inseed
414 type(random_stat) stat
415 call random_setseed_t(inseed,stat)
416 call random_index_t(imax,iharvest,stat)
421 subroutine random_index_s(imax,iharvest)
423 integer,
intent(in):: imax
424 integer,
intent(out):: iharvest(:)
425 if(sstat%mti.eq.n+1)
call random_setseed_t(iseed,sstat)
426 call random_index_t(imax,iharvest,sstat)
432 subroutine random_index_t(imax,iharvest,stat)
434 integer,
intent(in):: imax
435 integer,
intent(out):: iharvest(:)
436 type(random_stat),
intent(inout):: stat
437 integer,
parameter:: mh=n
443 call random_number_t(h(:i2-(i1-1)),stat)
444 iharvest(i1:i2)=max(ceiling(h(:i2-(i1-1))*imax),1)
This module calculates random numbers using the Mersenne twister.
real function, public random_number_f()
Generates random numbers in functional mode.
subroutine, public random_seed(size, put, get, stat)
Sets and gets state; overloads Fortran 90 standard.
integer function, public random_index_f(imax)
Generates random indices in functional mode.
real function, public random_gauss_f()
Generates Gaussian random numbers in functional mode.