function S=snmd2(n,w,sigma,mu)

% SNMD2       Bi-variate normal mixture density random sample.
%
%             Example: load nmd2
%                      data=snmd2(100,wk,sigmak,muk); 
%                      kdedemo2
%
%             SNMD2(N,W,SIGMA,MU)
%
%             Christian C. Beardah 1996

% randn('seed',sum(100*clock));

b(1)=w(1);
for i=2:length(w),
  b(i)=b(i-1)+w(i);
end;

for i=1:n,
  r=rand;
  for j=1:length(w),
    if r<b(j),
      S(i,1:2)=(sqrtm(sigma(1:2,(2*j-1):2*j))*randn(2,1)+mu(:,j))';
      break;
    end;
  end;
end;