Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::discrete_distribution() allow zero weights?

Tags:

c++

random

Can std::discrete_distribution be used with some zero weights, assuming that at least one positive weight was given? The items with zero weights should simply never be sampled.

The fact that it appears to work on my machine does not give me confidence that it will work on all systems.

Example:

std::discrete_distribution<> dd({1.0, 2.0, 0.0, 3.0});
like image 713
Szabolcs Avatar asked Apr 15 '26 11:04

Szabolcs


1 Answers

Per [rand.dist.samp.discrete]/2

Unless specified otherwise, the distribution parameters are calculated as: pk = wk / S for k = 0, ..., n−1, in which the values wk, commonly known as the weights, shall be non-negative, non-NaN, and non-infinity. Moreover, the following relation shall hold: 0 < S = w0 + ... + wn−1.

So zero weights are allowed, but not all of the weights can be zero since S must be greater than zero.

like image 169
NathanOliver Avatar answered Apr 18 '26 02:04

NathanOliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!