cxx_std 14
I need to create a random number generator whose distribution matches a given histogram. The values of the histogram are stored in a file.
std::random_device rd;
std::default_random_engine dre(rd());
std::vector<int> weights;
// these histogram numbers will be read from a file (about 400 of them)
weights.push_back(10);
weights.push_back(20);
weights.push_back(30);
weights.push_back(40);
weights.push_back(10);
std::discrete_distribution<int> discrete_dist(weights); // this is line saying that it doesn't know how to take a vector like a good bi**h
G4double random_number;
for(int n=0; n < 1000; ++n) {
random_number =
min_num + ((discrete_dist(dre) + 1) * (max_num - min_num) / intervals_count );
random_numbers->push_back(random_number);
error on compilation.
error: no matching function for call to 'std::discrete_distribution<int>::discrete_distribution(std::vector<int>&)'
32 | std::discrete_distribution<int> discrete_dist(weights);
This is how the documentation says it is to be done.
#include <iostream>
#include <map>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::discrete_distribution<> d({40, 10, 10, 40});
std::map<int, int> m;
for(int n=0; n<10000; ++n) {
++m[d(gen)];
}
for(auto p : m) {
std::cout << p.first << " generated " << p.second << " times\n";
}
}
It seems weird that this could only be used by hardcoding the distribution. Any help will be appreciated, thank you.
PS: typecasting to std::discrete_distribution<int>::param_type
did not help as it didn't know how to convert vector to that.
Aucun commentaire:
Enregistrer un commentaire