This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of Resolved status.
Section: 26.6.8.6.1 [rand.dist.samp.discrete] Status: Resolved Submitter: P.J. Plauger Opened: 2008-02-09 Last modified: 2016-02-10
Priority: Not Prioritized
View all other issues in [rand.dist.samp.discrete].
View all issues with Resolved status.
Discussion:
discrete_distribution should have a constructor like:
template<class _Fn> discrete_distribution(result_type _Count, double _Low, double _High, _Fn& _Func);
(Makes it easier to fill a histogram with function values over a range.)
[ Bellevue: ]
How do you specify the function so that it does not return negative values? If you do it is a bad construction. This requirement is already there. Where in each bin does one evaluate the function? In the middle. Need to revisit tomorrow.
[ Sophia Antipolis: ]
Bill is not requesting this.
Marc Paterno: _Fn cannot return negative values at the points where the function is sampled. It is sampled in the middle of each bin. _Fn cannot return 0 everywhere it is sampled.
Jens: lambda expressions are rvalues
Add a library issue to provide an initializer_list<double> constructor for discrete_distribution.
Marc Paterno: dislikes reference for _Fn parameter. Make it pass-by-value (to use lambda), use std::ref to wrap giant-state function objects.
Daniel: See random_shuffle, pass-by-rvalue-reference.
Daniel to draft wording.
[ Pre San Francisco, Daniel provided wording: ]
The here proposed changes of the WP refer to the current state of N2691. During the Sophia Antipolis meeting two different proposals came up regarding the functor argument type, either by value or by rvalue-reference. For consistence with existing conventions (state-free algorithms and the general_pdf_distribution c'tor signature) the author decided to propose a function argument that is provided by value. If severe concerns exists that stateful functions would be of dominant relevance, it should be possible to replace the two occurrences of Func by Func&& in this proposal as part of an editorial process.
Proposed resolution:
Non-concept version of the proposed resolution
In 26.6.8.6.1 [rand.dist.samp.discrete]/1, class discrete_distribution, just before the member declaration
explicit discrete_distribution(const param_type& parm);
insert:
template<typename Func> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
Between p.4 and p.5 insert a series of new paragraphs as part of the new member description::
template<typename Func> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);Complexity: Exactly nf invocations of fw.
Requires:
- fw shall be callable with one argument of type double, and shall return values of a type convertible to double;
- If nf > 0, the relation x_{min} < x_{max} shall hold, and for all sample values x_{k}, fw(x_{k}) shall return a weight value w_{k} that is non-negative, non-NaN, and non-infinity;
- The following relations shall hold: nf ≥ 0, and 0 < S = w_{0}+. . .+w_{n-1}.
Effects:
- If nf == 0, sets n = 1 and lets the sequence w have length n = 1 and consist of the single value w_{0} = 1.
Otherwise, sets n = nf, deltax = (x_{max} - x_{min})/n and x_{cent} = x_{min} + 0.5 * deltax.
For each k = 0, . . . ,n-1, calculates:
x_{k} = x_{cent} + k * deltax w_{k} = fw(x_{k})
Constructs a discrete_distribution object with probabilities:
p_{k} = w_{k}/S for k = 0, . . . , n-1.
Concept version of the proposed resolution
In 26.6.8.6.1 [rand.dist.samp.discrete]/1, class discrete_distribution, just before the member declaration
explicit discrete_distribution(const param_type& parm);
insert:
template<Callable<auto, double> Func> requires Convertible<Func::result_type, double> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
Between p.4 and p.5 insert a series of new paragraphs as part of the new member description::
template<Callable<auto, double> Func> requires Convertible<Func::result_type, double> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);Complexity: Exactly nf invocations of fw.
Requires:
- If nf > 0, the relation x_{min} < x_{max} shall hold, and for all sample values x_{k}, fw(x_{k}) shall return a weight value w_{k} that is non-negative, non-NaN, and non-infinity;
- The following relations shall hold: nf ≥ 0, and 0 < S = w_{0}+. . .+w_{n-1}.
Effects:
- If nf == 0, sets n = 1 and lets the sequence w have length n = 1 and consist of the single value w_{0} = 1.
Otherwise, sets n = nf, deltax = (x_{max} - x_{min})/n and x_{cent} = x_{min} + 0.5 * deltax.
For each k = 0, . . . ,n-1, calculates:
x_{k} = x_{cent} + k * deltax w_{k} = fw(x_{k})
Constructs a discrete_distribution object with probabilities:
p_{k} = w_{k}/S for k = 0, . . . , n-1.
Rationale:
Addressed by N2836 "Wording Tweaks for Concept-enabled Random Number Generation in C++0X".