function [ key1, key2 ] = phrtsd ( phrase )

%*****************************************************************************80
%
%% phrtsd() converts a phrase to a pair of random number generator keys.
%
%  Discussion:
%
%    This procedure uses a character string to generate two keys for the RGN
%    random number generator.
%
%    Trailing blanks are eliminated before the keys are generated.
%
%    Generated key values will fall in the range 1 to 2^30 = 1,073,741,824.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    03 September 2018
%
%  Author:
%
%    Original FORTRAN77 version by Barry Brown, James Lovato.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    string PHRASE, a phrase to be used for the random number generation.
%
%  Output:
%
%    integer key1, key2, the two keys for the
%    random number generator, based on PHRASE.
%
  table = strcat ( ...
    'abcdefghijklmnopqrstuvwxyz', ...
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ...
    '0123456789', ...
    '%@#$%^...*()_+[];:''"<>?,./' );

  twop30 = 1073741824;

  shift = [ 1, 64, 4096, 262144, 16777216 ];

  key1 = 1234567890;
  key2 = 123456789;

  values = zeros ( 5, 1 );

  lphr = lennob ( phrase );

  for i = 1 : lphr

    c = phrase(i);
    ichr = find ( c == table );

    if ( isempty ( ichr ) )
      ichr = 0;
    end

    ichr = mod ( ichr, 64 );

    if ( ichr == 0 )
      ichr = 63;
    end

    for j = 1 : 5
      values(j) = ichr - j;
      if ( values(j) < 1 )
        values(j) = values(j) + 63;
      end
    end

    for j = 1 : 5
      key1 = mod ( key1 + shift(j) * values(j), twop30 );
      key2 = mod ( key2 + shift(j) * values(6-j), twop30 );
    end

  end

  return
end
