c++ - Problems with std::piecewise_constant_distribution and std::vector -
given bunch of strings i'm trying create program can mimic pseudo-random behaviour weighted distribution based on input.
so far came this
#include <iostream> #include <random> #include <type_traits> #include <map> #include <vector> #include <string> #include <initializer_list> #define n 100 int main() { std::vector<std::string> interval{"bread", "castle", "sun"}; std::vector<float> weights { 0.40f, 0.50f, 0.10f }; std::piecewise_constant_distribution<> dist(interval.begin(), interval.end(), weights.begin()); std::random_device rd; std::mt19937 gen(rd()) ; for(int = 0; i<n;i++) { std::cout << dist(gen) << "\n"; } return(0); }
but thing doesn't works , have no clue why, usual usage of std::piecewise_constant_distribution
, according online examples, it's std::arrays
, i'm trying implement using std::vector
, main difference found.
with clang++ output of error is
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/random.tcc:2409:10: error: no matching member function call 'push_back' _m_int.push_back(*__bbegin); ~~~~~~~^~~~~~~~~
but can't understand because there no explicit .push_back
in code, don't coming because debugging templated class it's nightmare , i'm starting one.
anyone having idea why code doesn't work ?
the default result type of std::piecewise_constant_distribution
realtype
(double
here). seems you're trying choose 3 string
options various weights, isn't std::piecewise_constant_distribution
for. it's designed generate uniform random values numeric intervals given weighting. if modify example , change interval
to:
std::vector<double> interval {1, 3, 5, 10, 15, 20};
everything compile happily. looks of it, want std::discrete_distribution
:
... std::vector<std::string> interval{"bread", "castle", "sun"}; std::vector<float> weights { 0.40f, 0.50f, 0.10f }; std::discrete_distribution<> dist(weights.begin(), weights.end()); std::mt19937 gen(rd()); for(int = 0; i<n;i++) { std::cout << interval[dist(gen)] << "\n"; } ...
Comments
Post a Comment