function index=selectrand(p) % select with probability = p index=1; cs=0; r=rand; p=p/sum(p); while(cs+p(index)