function retval=monty(iter,swap) pwin=0; pfail=0; for i=1:iter door(1)=floor(2*rand); if door(1)==1 door(2)=0; door(3)=0; else door(2)=floor(2*rand); if door(2)==1 door(3)=0; else door(3)=1; endif endif contestant=floor(3*rand)+1; if door(contestant)==1 montychoice=coinflip(complement(create_set(contestant),create_set([1 2 3]))); else for j=1:3 if j~=contestant && door(j)==0 montychoice=j; j=3; endif endfor endif if swap==0 if door(contestant)==1 pwin=pwin+1; else pfail=pfail+1; endif else if swap==1 contestant=coinflip(complement(create_set(montychoice),create_set([1 2 3]))); if door(contestant)==1 pwin=pwin+1; else pfail=pfail+1; endif else contestant=coinflip(complement(create_set([montychoice contestant]),create_set([1 2 3]))); if door(contestant)==1 pwin=pwin+1; else pfail=pfail+1; endif endif endif endfor retval=[pwin/iter pfail/iter (pwin+pfail)/iter]; endfunction