/cxx-distr

Efficient discrete random distribution for C++. The implementation uses array-based sum tree to allow fast sampling and updates of weighted events.

Primary LanguageC++Boost Software License 1.0BSL-1.0

Efficient discrete random distribution for C++

Test Status C++11 Boost License

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.

Usage

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);
}

Testing

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

Project Status

Basic features are done.

License

Boost Software License, Version 1.0.