sample=function(n,mu0=0,mu1=0,sd1=1,sd2=sqrt(100),prob=0.05) { xx = vector() for(i in 1:n){ b = runif(1, 0, 1) if(b < prob){ xx[i] = rnorm(1, mu1, sd2) } else { xx[i] = rnorm(1, mu0, sd1) } } xx } ########################## n=50; mn=c();md=c(); for(j in 1:10000) { x=sample(n) mn[j]=mean(x) md[j]=median(x) } var(mn) var(md)