A method of choosing a line randomly from a file:

 Without reading the file more than once
 When substantial parts of the file cannot be held in memory
 Without knowing how many lines are in the file
Is to:

 keep the first line of the file as a possible choice, then
 Read the second line of the file if possible and make it the possible choice if a uniform random value between zero and one is less than 1/2.
 Read the third line of the file if possible and make it the possible choice if a uniform random value between zero and one is less than 1/3.
 ...
 Read the Nth line of the file if possible and make it the possible choice if a uniform random value between zero and one is less than 1/N
 Return the computed possible choice when no further lines exist in the file.


Task

 Create a function/method/routine called one_of_n that given n, the number of actual lines in a file, follows the algorithm above to return an integer - the line number of the line chosen from the file. 
The number returned can vary, randomly, in each run.
 Use one_of_n in a simulation to find what woud be the chosen line of a 10 line file simulated 1,000,000 times.
 Print and show how many times each of the 10 lines is chosen as a rough measure of how well the algorithm works.


Note: You may choose a smaller number of repetitions if necessary, but mention this up-front.

Note: This is a specific version of a Reservoir Sampling algorithm: https://en.wikipedia.org/wiki/Reservoir_sampling

 
!> read lines one at a time and randomly choose one
!! using a Reservoir Sampling algorithm:
!! http://www.rosettacode.org/wiki/One_of_n_lines_in_a_file
program reservoir_sample
use, intrinsic :: iso_fortran_env, only : dp=>real64
implicit none
character(len=256)   :: line
integer              :: lun, n, i, count(10)
   call random_seed()
   !! create test file
   open(file='_data.txt',newunit=lun)
   do i=1,10
      write(lun,'(*(g0))')'test line ',i
   enddo
   !! run once and show result
   call one_of_n(line,n)
   write(*,'(i10,":",a)')n,trim(line)
   !! run 1 000 000, times on ten-line test file
   count=0
   do i=1,1000000
      call one_of_n(line,n)
      if(n.gt.0.and.n.le.10)then
         count(n)=count(n)+1
      else
         write(*,*)'<ERROR>'
      endif
   enddo
   write(*,*)count
   write(*,*)count-100000
contains
subroutine one_of_n(line,n)
character(len=256),intent(out) :: line
integer,intent(out)            :: n
real(kind=dp)                  :: rand_val
integer                        :: ios, ilines
   line=''
   ios=0
   ilines=1
   n=0
   rewind(lun)
   do
      call random_number(rand_val)
      if( rand_val < 1.0d0/(ilines) )then
         read(lun,'(a)',iostat=ios)line
         if(ios/=0)exit
         n=ilines
      else
         read(lun,'(a)',iostat=ios)
         if(ios/=0)exit
      endif
      ilines=ilines+1
   enddo
end subroutine one_of_n
end program reservoir_sample
}