Algorithm for sampling without replacement?

前端 未结 6 1631
情歌与酒
情歌与酒 2020-12-02 13:56

I am trying to test the likelihood that a particular clustering of data has occurred by chance. A robust way to do this is Monte Carlo simulation, in which the associations

6条回答
  •  情歌与酒
    2020-12-02 14:23

    A C++ working code based on the answer by John D. Cook.

    #include 
    #include 
    
    double GetUniform()
    {
        static std::default_random_engine re;
        static std::uniform_real_distribution Dist(0,1);
        return Dist(re);
    }
    
    // John D. Cook, https://stackoverflow.com/a/311716/15485
    void SampleWithoutReplacement
    (
        int populationSize,    // size of set sampling from
        int sampleSize,        // size of each sample
        std::vector & samples  // output, zero-offset indicies to selected items
    )
    {
        // Use Knuth's variable names
        int& n = sampleSize;
        int& N = populationSize;
    
        int t = 0; // total input records dealt with
        int m = 0; // number of items selected so far
        double u;
    
        while (m < n)
        {
            u = GetUniform(); // call a uniform(0,1) random number generator
    
            if ( (N - t)*u >= n - m )
            {
                t++;
            }
            else
            {
                samples[m] = t;
                t++; m++;
            }
        }
    }
    
    #include 
    int main(int,char**)
    {
      const size_t sz = 10;
      std::vector< int > samples(sz);
      SampleWithoutReplacement(10*sz,sz,samples);
      for (size_t i = 0; i < sz; i++ ) {
        std::cout << samples[i] << "\t";
      }
    
      return 0;
    }
    

提交回复
热议问题