Opened 22 years ago
Closed 12 years ago
#920 closed Feature Requests (fixed)
Proposed extension to random library
| Reported by: | nobody | Owned by: | jmaurer |
|---|---|---|---|
| Milestone: | Component: | random | |
| Version: | None | Severity: | Problem |
| Keywords: | Cc: |
Description
I suggest adding the following utility function to
the random library (with perhaps a name change):
// PURPOSE: Sample from a set of N values given the
// sequence p[1], p[1] + p[2], p[1] + p[2] + p[3], ...
// of cumulative probabilities.
//
// REQUIRE:
// - Type Incrementer supports the pre-increment
// operation and equality comparison. Typically,
// Incrementer will be an integral type or a
// forward iterator.
// - Pre-incrementing i some finite number of times
// -- say, N -- makes it equal to iend.
// - cumprob_it "points" at a nondecreasing sequence
// of at least N nonnegative numbers, with the N-th
// value in the sequence being equal to 1.0.
//
// RETURNS:
// A random value in the interval [i, iend), with
// cumprob_it defining the sequence of cumulative
// probabilities.
template <typename InputIterator,
typename Incrementer,
class UniformRandomNumberGenerator>
Incrementer
rand(Incrementer i, Incrementer iend,
InputIterator cumprob_it,
UniformRandomNumberGenerator & rng)
{
typedef
std::iterator_traits<InputIterator>::value_type
number_t;
uniform_01<UniformRandomNumberGenerator, number_t>
urng(rng);
number_t p = urng();
for ( ; p >= *cumprob_it; ++cumprob_t) {
++i;
assert(!(i == iend));
}
return i;
}
-- Kevin S. Van Horn (Kevin_VanHorn@ndsu.nodak.edu)
Change History (4)
comment:2 by , 13 years ago
| Severity: | → Showstopper |
|---|
The new standard will contain a discrete_distribution, which returns an integer given a sequence of weights.
comment:3 by , 13 years ago
| Severity: | Showstopper → Problem |
|---|
Note:
See TracTickets
for help on using tickets.
