function X=rndzip(gen,varargin)
% rndzip - Random sequence by zipping argument sequences
%
% rndzip ::
%    (A1, ..., An -> rndgen B) ~'sampling function',
%    seq A1, ..., seq An       ~'argument sequences', 
%    rndstate                  ~'initial random state'
% -> seq B.

	n=length(varargin)-1;
	if n>0
		X=zipaccum(@zipfn,varargin{n+1},varargin{1:n});
	else
		X=unfold(@ufn,varargin{1});
	end

	function [x,s]=ufn(s0)
		setrndstate(s0);
		x=gen();
		s=getrndstate;
	end

	function [x,s]=zipfn(varargin)
		setrndstate(varargin{n+1});
		x=gen(varargin{1:n});
		s=getrndstate;
	end
end
	


