Header-only library providing cxx::discrete_distribution
class for C++11
and later. The class uses array-based sum tree to allow efficient sampling
and updates of discrete events, making it suitable for dynamic simulations like
Kinetic Monte Carlo and Gillespie algorithm.
Download discrete_distribution.hpp and include in your program. The library is single header-only and has zero dependency.
#include <discrete_distribution.hpp>
int main()
{
// Distribution of four events (0, 1, 2 or 3) with given weights.
cxx::discrete_distribution<int> distr = {1.2, 3.4, 5.6, 7.8};
std::mt19937_64 random;
// 0, 1, 2 or 3 is chosen.
int choice = distr(random);
// Change the weight of the event 1 to zero.
distr.update(1, 0.0);
// Now event 1 will not be chosen because it has zero weight.
choice = distr(random);
}
To run unit tests:
git clone https://github.com/snsinfu/cxx-distr.git
cd cxx-distr/test
make
Also you can test example program:
cd cxx-distr/example
make -C gillespie
make -C random_network
Basic features are done.
Boost Software License, Version 1.0.