I am writing a fortran 95 code for Brownian dynamics. So I need to insert random numbers at each time-step. To generate the random numbers or ‘noise’, which needs to be Gaussian white noise, I am calling a subroutine which employs the Box-Muller algorithm as follows –
subroutine box_muller(x)
implicit none
real,intent(out) :: x
real,parameter :: pi=3.14159265
real :: u1,u2
! Box-Muller
call random_number(u1)
call random_number(u2)
x = sqrt(-2*log(u1))*cos(2*pi*u2)
end subroutine
I wrongly inserted the seed as follows –
subroutine box_muller_mod(x)
implicit none
integer :: n
integer,allocatable :: seed(:)
real,intent(out) :: x
real,parameter :: pi=3.14159265
real :: u1,u2
! setting seed
call random_seed(size=n) ! n is processor-dependent
allocate(seed(n))
seed = 120000
call random_seed(put=seed) ! effectively initializing the RNG with the seed
! Box-Muller
call random_number(u1)
call random_number(u2)
x = sqrt(-2*log(u1))*cos(2*pi*u2)
end subroutine
It gave the exact same (random!) number for the whole run.
But I need to use “seed” so that once declared a particular seed in the beginning of the script, it will use a particular sequence of random numbers in the whole run of the code.
If needed I shall be able to change the initial seed and the code will use a different sequence of random numbers this time.
The question is how to do this?