std::discre...distribution(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

hey bestie πŸ’– welcome to std::discrete_distribution::discrete_distribution β€” the constructor page from the C++ Standard Libary manual (yes, that spelling is the manual name, not me typoing, periodt πŸ’€). You might see it squished in the header as std::discre...distribution(3), but the actual star of the show is the std::discrete_distribution constructor. It's lowkey giving β€œmake a distribution object” energy, and honestly? highkey iconic. This page ate and left no crumbs, fr fr. 🧚✨

🌸 NAME 🌸

std::discrete_distribution::discrete_distribution β€” std::discrete_distribution::discrete_distribution. That's literally the constructor page, bestie. No cap. πŸŽ€

πŸ¦‹ Synopsis πŸ¦‹

Vibe check: all five are (since C++11) and they're serving:

discrete_distribution();

(1) (since C++11)

template< class InputIt >
discrete_distribution( InputIt first, InputIt last );

(2) (since C++11)

discrete_distribution( std::initializer_list<double> weights );

(3) (since C++11)

template< class UnaryOperation >
discrete_distribution( std::size_t count, double xmin, double xmax,
                       UnaryOperation unary_op );

(4) (since C++11)

explicit discrete_distribution( const param_type& params );

(5) (since C++11)

Constructs a new distribution object. 🫢

Here's the tea on each overload:

  1. Default constructor. Constructs the distribution with a single weight p = {1}. This distribution will always generate 0. (ngl, one-trick pony but reliable, honestly πŸ’…)
  2. Constructs the distribution with weights in the range [first, last). If first == last, the effects are the same as of the default constructor.
  3. Constructs the distribution with weights in weights. Effectively calls discrete_distribution(weights.begin(), weights.end()).
  4. Constructs the distribution with count weights that are generated using function unary_op. Each of the weights is equal to w_i = unary_op(xmin + Ξ΄(i + 0.5)), where Ξ΄ = (xmax βˆ’ xmin) / count β€” yes, math, but make it cute 😭 β€” and i ∈ {0, ..., count βˆ’ 1}. xmin and xmax must be such that Ξ΄ > 0. If count == 0, the effects are the same as of the default constructor.
  5. Constructs the distribution with params as the distribution parameters.

πŸŽ€ Parameters πŸŽ€

  • first, last β€” the range of elements defining the numbers to use as weights. The type of the elements referred by InputIterator must be convertible to double.
  • weights β€” initializer list containing the weights.
  • unary_op β€” unary operation function object that will be applied. The signature of the function should be equivalent to the following:
Ret fun(const Type &a);

The signature does not need to have const &. The type Type must be such that an object of type double can be dereferenced and then implicitly converted to Type. The type Ret must be such that an object of type double can be dereferenced and assigned a value of type Ret.

  • params β€” the distribution parameter set.

🌈 Type requirements 🌈

  • InputIt must meet the requirements of LegacyInputIterator.

okay that's the whole page, bestie 🫢 source/version is http://cppreference.com, dated 2024.06.10. Go forth and construct something iconic, slay. πŸ’…βœ¨

πŸ“¦ where this page comes from