init_random_seed_by_system_clock Subroutine

public subroutine init_random_seed_by_system_clock()

NAME

init_random_seed_by_system_clock(3f) - [M_random] seed random_number(3f) with system clock value
(LICENSE:MIT)

SYNOPSIS

subroutine init_random_seed_by_system_clock()

DESCRIPTION

A simple wrapper around random_seed(3f) that uses the system clock to initialize the seed so you can
easily call random_number(3f) with varying pseudo-random real number sequences

EXAMPLE

Sample program:

 program demo_init_random_seed_by_system_clock
 use M_random, only : init_random_seed_by_system_clock
 integer :: i
 real    :: x
    call init_random_seed_by_system_clock()
    do i=1,10
       ! generate real pseudo-random numbers from 0 to <1.0
       call random_number(x)
       write(*,*)i,x
    enddo
 end program demo_init_random_seed_by_system_clock

Results

 >   1  0.661672294
 >   2  0.274969578
 >   3  0.683666587
 >   4   7.35652447E-02
 >   5  0.457893968
 >   6  0.826303899
 >   7  0.727411628
 >   8  0.542535722
 >   9  0.983459771
 >  10  0.527638793

AUTHOR

John S. Urban

LICENSE

MIT License

Arguments

None

Source Code

subroutine init_random_seed_by_system_clock()

! ident_5="@(#) M_random init_random_seed_by_system_clock(3f) initialize random_number(3f) to return a single value with system clock"

   integer :: i, n, clock
   integer, dimension(:), allocatable :: seed
   call random_seed(size = n)
   if(allocated(seed))deallocate(seed)
   allocate(seed(n))
   call system_clock(count=clock)
   seed = clock + 37 * (/ (i - 1, i = 1, n) /)
!   write(*,*)seed
!   write(*,*)(/ (i - 1, i = 1, n) /)
   call random_seed(put = seed)
   deallocate(seed)
end subroutine init_random_seed_by_system_clock