!===============================================================================
!
! univ/ran.F90 - random numbers
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2008-2016 Hinnerk Stueben
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! BQCD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
RAN_NAME function ran_name()

  implicit none

  ran_name = "univ"
end

!-------------------------------------------------------------------------------
subroutine ran_init(seed)

  implicit none
  SEED, intent(in)  :: seed
  SEED              :: s8
  integer(2)        :: s2(4)
  integer           :: s4(4)
  equivalence         (s8, s2)

  s8 = seed
  s4(:) = s2(:)

  call univ_init(s4)
end

!-------------------------------------------------------------------------------
REAL function ran_number()

   implicit none
   real(8), external :: univ

   ran_number = univ()
end

!-------------------------------------------------------------------------------
subroutine ran_vector(n, x)

   implicit none
   integer, intent(in) :: n
   REAL, intent(out)   :: x(n)

   call univ_vec(x, n)
end

!-------------------------------------------------------------------------------
subroutine ran_skip(n)

  implicit none
  integer, intent(in) :: n
  integer             :: i
  REAL                :: dummy
  real(8), external   :: univ

  do i = 1, n
     dummy = univ()
  enddo
end

!-------------------------------------------------------------------------------
subroutine ran_dump(unit)

  use module_function_decl
  implicit none
  integer, intent(in) :: unit
  FILENAME, external :: ran_file

  call ran_check()
  call univ_dump(unit, ran_file())
end

!-------------------------------------------------------------------------------
subroutine ran_restore(unit)

  implicit none
  integer, intent(in) :: unit
  FILENAME, external :: ran_file

  call univ_restore(unit, ran_file())
end

!-------------------------------------------------------------------------------
SEED function ran_get_seed()

  implicit none

  ran_get_seed = -1
end

!-------------------------------------------------------------------------------
subroutine ran_check()

  use module_function_decl
  use module_univ
  implicit none
  integer(8) :: state0(102)
  integer    :: i

  if (my_pe() == 0) then
     state0(:) = state(:) 
  else 
     state0(:) = -1
  endif

  call comm_broadcast_i8(state0, 102)

  do i = 1, 102
     ASSERT(state(i) == state0(i))
  enddo
end

!===============================================================================
