Program Listing for File binned_powerlaw_distribution.hpp

Return to documentation for file (rcppsw/math/binned_powerlaw_distribution.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "rcppsw/math/powerlaw_distribution.hpp"
#include "rcppsw/math/rng.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace rcppsw::math {

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class binned_powerlaw_distribution : public powerlaw_distribution {
 public:
  binned_powerlaw_distribution(size_t lb, size_t ub, size_t pwr)
      : powerlaw_distribution(lb, ub, pwr) {}

  size_t operator()(rng* const rng) {
    auto sample = static_cast<size_t>(powerlaw_distribution::operator()(rng));

    /* Round to the nearest multiple of the power for the distribution */
    auto multiple = static_cast<size_t>(std::pow(pwr(), sample));
    auto remainder = sample % multiple;
    if (0 == remainder) {
      return sample;
    } else {
      return sample + multiple - remainder;
    }
  }
};

} /* namespace rcppsw::math */