samedi 14 octobre 2017

2D random points under a curve in c++

I want to choose 50 random points between x-axis and curve y=sin (x)+2 (for example). I want random points to be spread uniformly in this 2D region. I did like this:

using namespace std;
double randnum (double aa, double bb)  //defining a function to create random numbers
{
  static std::default_random_engine generator;
  std::uniform_real_distribution<double> distribution (aa,bb);
  return distribution(generator);
}
   for(int i = 0; i < 50; i++) 
            {
            x[i] = randnum(0,2 * M_PIl);
            y[i] = randnum(0,sin(x[i])+2);
            } 

But it is not correct because it gives points denser in regions which curve is closer to the x-axis. How can I choose points with equal density in the domain?




Aucun commentaire:

Enregistrer un commentaire