function [ line, line_index, line_num ] = file_line_uniform ( file_name )

%*****************************************************************************80
%
%% file_line_uniform() returns a random line from a file.
%
%  Discussion:
%
%    The file is assumed to be a simple text file.
%
%    The algorithm used is interesting because it does not require
%    the number of lines in the file to be known in advance, and it
%    only reads the file once.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    20 June 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Tom Christiansen, Nathan Torkington,
%    "8.6: Picking a Random Line from a File",
%    Perl Cookbook, pages 284-285,
%    O'Reilly, 1999.
%
%  Input:
%
%    string FILE_NAME, the name of the file.
%
%  Output:
%
%    string LINE, a random line from the file.
%
%    integer LINE_INDEX, the index of the chosen line.
%
%    integer LINE_NUM, the number of lines in the file.
%
  line_num = 0;
  line_index = -1;
  line = ' ';
  line_read = ' ';
%
%  Open the file.
%
  input_unit = fopen ( file_name );

  if ( input_unit < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'file_line_uniform(): Error!\n' );
    fprintf ( 1, '  Could not open the file "%s%.\n', file_name );
    error ( 'file_line_uniform(): Error!' );
    return;
  end
%
%  Read the lines.
%
  while ( true )

    line_read = fgetl ( input_unit );

    if ( line == -1 )
      break;
    end

    line_num = line_num + 1;

    r = rand ( 1, 1 );

    if ( r * line_num <= 1.0 )

      line = line_read;
      line_index = line_num;

    end

  end

  fclose ( input_unit );

  return
end
