Ask Your Question

How to generate a random permutation?

asked 2012-12-15 08:07:04 -0600

Breeze gravatar image

updated 2012-12-16 07:35:26 -0600

Adi gravatar image

I am using opencv2 to generate a random permutation.
For example, generate a random permutation with number ranging from 1 to 10. I have tried many times, howerver ,I couldn't realize it it.
Would be great to get your help. I appreciate any suggestions

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-08-12 02:46:33 -0600

SR gravatar image
#include <algorithm>    // std::random_shuffle
#include <vector>       // std::vector
#include <cstdlib>      // std::rand, std::srand

vector<int> permutation = 1, 2, 3, ..., 10
std::random_shuffle(permutation.begin(), permutation.end());
edit flag offensive delete link more

answered 2012-12-16 10:07:50 -0600

Adi gravatar image

You can use the Boost Permutation Iterator or use OpenCV's randShuffle() (there is an example of how to use it in the samples under samples\cpp\kmeans.cpp).

edit flag offensive delete link more

Question Tools


Asked: 2012-12-15 08:07:04 -0600

Seen: 2,094 times

Last updated: Aug 12 '13