-2

I have the below Fortran code:

program Looops
  implicit none 
  
  integer, parameter:: PPPP = 3.1415     
  real*8 , Dimension(:) , allocatable  ::dkk25,dkk26,dkk27,dkk28 
  integer:: n_38
  
  Allocate(dkk25(40))
  Allocate(dkk26(40))
  Allocate(dkk27(40))
  Allocate(dkk28(40))

  call RANDOM_NUMBER(dkk25)
  call RANDOM_NUMBER(dkk26)
  dkk27(:) = sqrt(-2*log((dkk25(:))))*cos(2*pppp*(dkk26(:)))

  do n_38 = 1 , 40
    dkk28(n_38) = ((dkk27(n_38)-(sum(dkk27)/40))**2)/(40-1)
  end do
  
  print*,dkk25
end program Looops

In each run of this code I have same value of dkk25 and dkk26. I want to generate different random number in each run.

1 Answers1

0

Use RANDOM_SEED (with no arguments).

From https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html :

8.225 RANDOM_SEED — Initialize a pseudo-random number sequence

Description: Restarts or queries the state of the pseudorandom number generator used by RANDOM_NUMBER.

If RANDOM_SEED is called without arguments, it is seeded with random data retrieved from the operating system.

Or use RANDOM_INIT if your compiler supports it.

L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28