public subroutine init_random_seed(mine)
NAME
init_random_seed(3f) - [M_random] seed random_number(3f) with single value like srand(3c) usage
(LICENSE:MIT)
SYNOPSIS
subroutine init_random_seed(mine)
integer,intent(in) :: mine
DESCRIPTION
A simple wrapper around random_seed(3f) that uses the single given
integer to initialize the seed so you can easily call random_number(3f)
with varying pseudo-random real number sequences simply, much like
srand(3c) and rand(3c).
EXAMPLE
Sample program:
program demo_init_random_seed
use M_random, only : init_random_seed
integer :: iseed
integer :: i
real :: x
iseed=218595421
call init_random_seed(iseed)
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
Results
> 1 0.989341617
> 2 0.296594143
> 3 0.805420995
> 4 4.00894880E-03
> 5 5.73359132E-02
> 6 0.805290103
> 7 0.944527864
> 8 0.789443851
> 9 0.327288270
> 10 0.710926533
AUTHOR
LICENSE
Arguments
Type |
Intent | Optional | Attributes |
|
Name |
|
integer,
|
intent(in) |
|
|
:: |
mine |
|
Source Code
subroutine init_random_seed(mine)
! ident_7="@(#) M_random init_random_seed(3f) initialize random_number(3f) to return a single value with single integer seed like srand(3c)"
! to make this start with a single number like srand(3c) take the seed and
! use the value to fill the seed array, adding 37 to each subsequent value
! till the array is filled.
integer,intent(in) :: mine
integer :: i, n
integer, dimension(:), allocatable :: seed
call random_seed(size = n)
if(allocated(seed))deallocate(seed)
allocate(seed(n))
seed = mine + 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