module initrs
  implicit none

contains
  integer function next_free_unit(n1,n2)
    integer n1,n2,i
    logical op,ex
    
    next_free_unit=-1
    do i=n1,n2
       inquire(unit=i,opened=op,exist=ex)
       if (.not.op.and.ex) then
          next_free_unit=i
          exit
       end if
    end do
  end function next_free_unit
 
  subroutine init_random_seed()
    integer n,un
    integer, allocatable :: seed(:)
    character(1), allocatable :: x(:)

    call random_seed(size=n)
    allocate(seed(n))
    allocate(x(n*4))

    un=next_free_unit(50,99)
    if (un<0) then
       write(*,*) 'blad w init_random_seed'
       write(*,*) 'brak wolnych urzadzen'
       stop
    end if
    open(un,file='/dev/urandom')
    read(un,'(a)') x
    close(un)
    seed=transfer(x,seed)
    
    call random_seed(put=seed)
    deallocate(x,seed)
  end subroutine init_random_seed

end module initrs
    
