function x = gauss_seidel_stochastic ( kits, A, b, x )

%*****************************************************************************80
%
%% gauss_seidel_stochastic() carries out stochastic Gauss-Seidel iteration.
%
%  Discussion:
%
%    The main interest of this code is that it is an understandable analogue to
%    the stochastic gradient descent method used for optimization in various
%    machine learning applications.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 February 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer KITS: the number of iterations to carry out.
%
%    real A(N,N): the matrix.
%
%    real B(N): the right hand side of the linear system.
%
%    real X(N): the solution estimate.
%
%  Output:
%
%    real X(N): the updated solution estimate.
%
  n = size ( A, 1 );

  for k = 1 : kits
    i = randi ( [ 1, n ] );
    x(i) = x(i) + ( b(i) - A(i,:) * x(:) ) / A(i,i);
  end

  return
end

