MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ weighted_pick() [2/2]

static size_t MayaFlux::Kinesis::Stochastic::Weights::weighted_pick ( std::span< const double >  weights,
Stochastic &  generator 
)
inlinestatic

Weighted random draw over a row of nonnegative weights.

Parameters
weightsRow to draw from. Negative entries are treated as zero.
generatorEntropy source for the draw
Returns
Index into weights, or weights.size() when every weight is zero or negative and no draw is possible

Does not require a Weights instance or normalized weights; any span of nonnegative values is a valid distribution to draw from.

Definition at line 252 of file Weights.hpp.

253 {
254 double total = 0.0;
255 for (double weight : weights) {
256 total += std::max(0.0, weight);
257 }
258
259 if (total <= 0.0) {
260 return weights.size();
261 }
262
263 const double target = generator(0.0, total);
264 double running = 0.0;
265 for (size_t i = 0; i < weights.size(); ++i) {
266 running += std::max(0.0, weights[i]);
267 if (target <= running) {
268 return i;
269 }
270 }
271 return weights.size() - 1;
272 }
uint32_t total
double weight

References total, and weight.

Referenced by MayaFlux::Nodes::Network::WalkOperator::advance(), and weighted_pick().

+ Here is the caller graph for this function: