thedarkside ofthemoon
thedarkside ofthemoon

Reputation: 2281

OpenCV: RNG generates the same numbers

I am using OpenCV and I use cv::RNG to generate pick randomly a percent from the images in the list:

void chooseImages(const StringVector& imagesListIn, StringVector& imagesListPartOut)
{
  imagesListPartOut.clear();

  if (m_perecent == 1)
  {
    imagesListPartOut = imagesListIn;
  }
  else
  {
    // copy the imagesListIn for being able to remove the chosed images
    int percent = static_cast<int>(m_perecent * imagesListIn.size() + .5);

    cv::RNG randomChooser;
    StringVector listCpy = imagesListIn;
    int index;
    int listSize = imagesListIn.size();
    std::string name;

    for (int i = 0; i < percent; i++)
    {
      index = randomChooser.next() % listSize;
      std::cout << index << " ";
      name = listCpy[index];
      listCpy.erase(listCpy.begin() + index);
      imagesListPartOut.push_back(name);
      listSize = listCpy.size();
    }
    std::cout << std::endl;
  }
}

My problem is that it always pick the same images; it always prints the same numbers (cv::RNG always generate the same numbers). How to make it generate different randomly umbers?

Upvotes: 2

Views: 3451

Answers (1)

Martin Beckett
Martin Beckett

Reputation: 96109

You need to seed a random number generator with a random starting state. See rng docs. Typically you use the computer's current time

Upvotes: 4

Related Questions