Following the documentation provided here I am trying to randomly permute a std::vector like this:
#include <vector>
#include <boost/random/linear_congruential.hpp>
#include <boost/range/algorithm.hpp>
int main(){
std::vector<int> vecint;
for(int i = 0; i < 10; i++)
vecint.push_back(i);
boost::minstd_rand gen(0);
boost::range::random_shuffle(vecint, gen);
}
This however does not compile. Godbolt link is here:
I think that the following condition specified in the documentation does not hold:
RandomAccessRange's distance type is convertible to Generator's argument type.
How can I find out the distance type of std::vector and the particular generator's argument type to figure out if they are compatible or not?
Following the documentation provided here I am trying to randomly permute a std::vector like this:
#include <vector>
#include <boost/random/linear_congruential.hpp>
#include <boost/range/algorithm.hpp>
int main(){
std::vector<int> vecint;
for(int i = 0; i < 10; i++)
vecint.push_back(i);
boost::minstd_rand gen(0);
boost::range::random_shuffle(vecint, gen);
}
This however does not compile. Godbolt link is here: https://godbolt./z/zjhh6r3xd
I think that the following condition specified in the documentation does not hold:
RandomAccessRange's distance type is convertible to Generator's argument type.
How can I find out the distance type of std::vector and the particular generator's argument type to figure out if they are compatible or not?
Share Improve this question asked Feb 17 at 11:29 One_Cable5781One_Cable5781 655 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 2That minstd_rand
is not generator functor but engine. Something like that works:
#include <iostream>
#include <vector>
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/range/algorithm.hpp>
int main() {
std::vector<int> vecint;
for(int i = 0; i < 10; i++)
vecint.push_back(i);
boost::minstd_rand engine(0);
boost::uniform_int<> distribution;
boost::variate_generator gen(engine, distribution);
boost::range::random_shuffle(vecint, gen);
boost::range::copy(vecint, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
std::shuffle
– One_Cable5781 Commented Feb 17 at 13:17std::shuffle
. – dalfaB Commented Feb 17 at 13:28