Program Listing for File Random.hpp

Return to documentation for file (include/networkit/auxiliary/Random.hpp)

/*
 * Random.hpp
 *
 *  Created on: 02.01.2014
 *      Author: FJW
 */

#ifndef NETWORKIT_AUXILIARY_RANDOM_HPP_
#define NETWORKIT_AUXILIARY_RANDOM_HPP_

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <random>
#include <stdexcept>

namespace Aux {

namespace Random {

void setSeed(uint64_t seed, bool useThreadId);

uint64_t getSeed();

std::mt19937_64 &getURNG();

uint64_t integer();
uint64_t integer(uint64_t upperBound);
uint64_t integer(uint64_t lowerBound, uint64_t upperBound);

double real();
double real(double upperBound);
double real(double lowerBound, double upperBound);

double probability();

std::size_t index(std::size_t max);

template <typename Container>
typename Container::const_reference choice(const Container &container) {
    return container[index(container.size())];
}

template <typename Element>
const Element &weightedChoice(const std::vector<std::pair<Element, double>> &weightedElements) {
    if (weightedElements.empty())
        throw std::runtime_error("Random::weightedChoice: input size equal to 0");
    double total = 0.0;
    for (const auto &entry : weightedElements) {
        assert(entry.second >= 0.0 && "This algorithm only works with non-negative weights");
        total += entry.second;
    }
    double r = Aux::Random::real(total);
    for (const auto &entry : weightedElements) {
        if (r < entry.second) {
            return entry.first;
        }
        r -= entry.second;
    }
    throw std::runtime_error(
        "Random::weightedChoice: should never get here"); // should never get here
}

} // namespace Random
} // namespace Aux

#endif // NETWORKIT_AUXILIARY_RANDOM_HPP_