function flag=runmartruin(s, j, p)
value=s;
stake=1;
while (value>0)&&(value<j)
    gamble=rand(1);
    if gamble<p  % and we assume p<1/2
        value=value+stake;
    else
        value=value-stake;
        stake=2*stake;
    end
end
if value<=0
    flag=1;
else
    flag=0;
end