Search code examples
c++11point-clouds

Random indices in Point cloud


I am trying to find random indices for selecting some points in point cloud. Following is the code. However, even after using srand() I am getting same number all three times. Can someone please help, regarding this?

/* find three points randomly */ 
for (long i = 0; i < 3; ++i) 
{
   srand (time(NULL));
   cout <<"\nRandom index" << (rand() % points.size() + 1); 
}

Solution

  • You are seeding your random generator with the same time, once each loop iteration.

    Instead seed it once at the start:

    /* find three points randomly */ 
    srand(time(NULL));
    for(int i = 0; i != 3; ++i) {
      cout <<"\nRandom index" << (rand() % points.size() + 1); 
    }
    

    You also don't need to use long for a loop of three steps :)