c++opencvc++11non-uniform-distribution

equivalent function to numpy.random.choice in C++


I need your help to solve the following problem:

Is there a function in c++/opencv which is equivalent to the following code:

np.random.choice(len(vec), samples, p=probabilities[:,0], replace=True)

Thanks in advance.


Solution

  • seems you are looking to sample from a discrete random distribution

    the example on that page is fairly demonstrative:

    // discrete_distribution
    #include <iostream>
    #include <random>
    
    int main()
    {
      const int nrolls = 10000; // number of experiments
      const int nstars = 100;   // maximum number of stars to distribute
    
      std::default_random_engine generator;
      std::discrete_distribution<int> distribution {2,2,1,1,2,2,1,1,2,2};
    
      int p[10]={};
    
      for (int i=0; i<nrolls; ++i) {
        int number = distribution(generator);
        ++p[number];
      }
    
      std::cout << "a discrete_distribution:" << std::endl;
      for (int i=0; i<10; ++i)
        std::cout << i << ": " << std::string(p[i]*nstars/nrolls,'*') << std::endl;
    
      return 0;
    }