function t = bal_seq_random ( n )

%*****************************************************************************80
%
%% bal_seq_random() selects a balanced sequence at random.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 June 2024
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Donald Kreher, Douglas Simpson,
%    Combinatorial Algorithms,
%    CRC Press, 1998,
%    ISBN: 0-8493-3988-X,
%    LC: QA164.K73.
%
%  Input:
%
%    integer N, the number of 0's (and 1's) in the sequence.
%    N must be positive.
%
%  Output:
%
%    integer T(2*N), a randomly chosen balanced sequence.
%

%
%  Count the balanced sequences.
%
  bal_seq_num = bal_seq_enum ( n );
%
%  Choose RANK between 1 and BAL_SEQ_NUM.
%
  rank = randi ( [ 1, bal_seq_num ], 1 );
%
%  Compute the balanced sequence of given rank.
%
  t = bal_seq_unrank ( rank, n );

  return
end
