Created
December 22, 2014 09:51
-
-
Save muupan/a6570982ba10241b3fb8 to your computer and use it in GitHub Desktop.
roulette wheel selection using std::discrete_distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <array> | |
#include <iostream> | |
#include <random> | |
int main() { | |
constexpr std::array<double, 3> fitness_values = {1, 1.5, 2}; | |
// Constructor is O(n) | |
std::discrete_distribution<std::size_t> dist(fitness_values.begin(), fitness_values.end()); | |
std::array<size_t, 3> table; | |
table.fill(0); | |
std::mt19937 random_engine; | |
for (auto i = 0; i < 100000; ++i) { | |
// Sampling is O(1) | |
++table[dist(random_engine)]; | |
} | |
for (auto i = 0; i < 3; ++i) { | |
std::cout << i << " was selected " << table[i] << " times." << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment