! code ---------------------------------------------------------------------------------------------------------------------------------
module random
integer(4),dimension(:),allocatable::defaultseed,randomseed
integer::seedsize
!----------------------------------------------------------------------------------------------------------
contains
!----------------------------------------------------------------------------------------------------------
subroutine allocateseeds()
call random_seed(size=seedsize)
allocate(defaultseed(seedsize))
allocate(randomseed(seedsize))
endsubroutine
!----------------------------------------------------------------------------------------------------------
subroutine setdefaultseed()
! Seeds the random number generator to a constant value.
integer::i
do i= 1,seedsize
defaultseed(i)=0
enddo
call random_seed(put=defaultseed)
endsubroutine
!----------------------------------------------------------------------------------------------------------
subroutine setrandomseed()
! Seeds the random number generator to a random value.
integer::cnt,cntrate,cntmax,i
do i= 1,seedsize
call system_clock(cnt,cntrate,cntmax)
randomseed(i)=cnt
enddo
call random_seed(put=randomseed)
endsubroutine
!----------------------------------------------------------------------------------------------------------
subroutine resetdefaultseed()
call random_seed(put=defaultseed)
endsubroutine
!----------------------------------------------------------------------------------------------------------
subroutine resetrandomseed()
call random_seed(put=randomseed)
endsubroutine
!----------------------------------------------------------------------------------------------------------
end module random
!----------------------------------------------------------------------------------------------------------
!----------------------------------------------------------------------------------------------------------
subroutine printrandomvals()
double precision::ranval
integer::i
do i=1,5
call random_number(ranval)
print *,ranval
enddo
print *
endsubroutine
!----------------------------------------------------------------------------------------------------------
!----------------------------------------------------------------------------------------------------------
program test
use random
implicit none
integer::sz
character::c
call random_seed(size=sz)
print *
print '(a,i1,a)', 'The random number generator takes a seed array consisting of ', sz, ' elements.'
print *
print *
call allocateseeds()
call setdefaultseed()
print *, 'The first 5 doubles in the default sequence.'
print *
call printrandomvals()
call setrandomseed()
print *, 'The first 5 doubles in a random sequence.'
print *
call printrandomvals()
call resetrandomseed()
print *, 'The first 5 doubles again in the same random sequence.'
print *
call printrandomvals()
call setrandomseed()
print *, 'The first 5 doubles in a new random sequence.'
print *
call printrandomvals()
call resetdefaultseed()
print *, 'The first 5 doubles in the default sequence again.'
print *
call printrandomvals()
print *
print *, 'Enter a character to quit.'
print *
read *, c
end program test
! output -------------------------------------------------------------------------------------------------------------------------------
The random number generator takes a seed array consisting of 8 elements.
The first 5 doubles in the default sequence.
0.30901699450542608
0.80380552047952203
0.35249806832347486
0.99809236009560809
0.55022038741740864
The first 5 doubles in a random sequence.
0.35368386010621311
0.92495087746258120
2.79358793357609159E-002
0.43250869170195372
0.79918821411338958
The first 5 doubles again in the same random sequence.
0.35368386010621311
0.92495087746258120
2.79358793357609159E-002
0.43250869170195372
0.79918821411338958
The first 5 doubles in a new random sequence.
0.85485606414247872
0.22150034995476942
0.86076915663017317
0.17166310760556669
0.48618469153662536
The first 5 doubles in the default sequence again.
0.30901699450542608
0.80380552047952203
0.35249806832347486
0.99809236009560809
0.55022038741740864
Enter a character to quit.
Bookmarks