publicclassReservoirSamplingTest{privateint[]pool;// 所有数据privatefinalintN=100000;// 数据规模privateRandomrandom=newRandom();@BeforepublicvoidsetUp()throwsException{// 初始化pool=newint[N];for(inti=0;i<N;i++){pool[i]=i;}}privateint[]sampling(intK){int[]result=newint[K];for(inti=0...
1、构建一个可放m个元素的蓄水池,将序列的前m个元素放入蓄水池中; 2、从第m+1个元素开始,以的概率来决定该元素是否被替换到池子中; 3、当遍历完所有元素之后,蓄水池中的就是随机挑选出的m个元素。 算法伪代码为: for i= m+1 to N k=random(1, i); if( k < m) SWAP the kth value and ith ...
voidInsideOutShuffle(conststd::vector<int>&src,std::vector<int>&result){result.assign(result.size(),0);std::copy(src.begin(),src.end(),result.begin());intk;for(unsignedinti=0;i<src.size();++i){k=rand()%(i+1);result[i]=result[k];result[k]=src[i];}} 蓄水池抽样 从长度为...