function cr_index = cr_index_choose ( cr_num, cr_prob )

%*****************************************************************************80
%
%% cr_index_choose() chooses a CR value.
%
%  Discussion:
%
%    Index I is chosen with probability CR_PROB(I).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    24 January 2018
%
%  Author:
%
%    Original FORTRAN90 version by Guannan Zhang.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    integer CR_NUM, the total number of CR values.
%    1 <= CR_NUM.
%
%    real CR_PROB(CR_NUM), the probability of each CR.
%
%  Output:
%
%    integer CR_INDEX, the index of the CR.
%    1 <= CR_INDEX <= CR_NUM.
%
  if ( cr_num == 1 )

    cr_index = 1

  else
 
    n = 1;
    tmp_index = i4vec_multinomial_sample ( n, cr_prob, cr_num );

    for i = 1 : cr_num
      if ( tmp_index(i) == 1 )
        cr_index = i;
        break
      end
    end

  end

  return
end
