long cls_random::randomNegativeBinomial( double r, double probability) { long rnd = 0; double fr = 0.0; while(true) { double pV = (double)rand()/(double)RAND_MAX; if (pV<probability) { rnd++; } else { fr++; } if (fr>r) { break; } } return rnd; }
期望:E=r*(1-p)/p
方差:V=r*(1-p)/(p*p)
wiki: http://zh.wikipedia.org/wiki/%E8%B4%9F%E4%BA%8C%E9%A1%B9%E5%88%86%E5%B8%83