An integer weight expresses an outcome’s share of a discrete distribution without requiring its probability to be stored as a fraction. For an outcome in the support, divide its positive weight by the sum of all support weights to get its sampling probability. An outcome outside the support has weight zero. Weights therefore specify ratios: 1 and 3 describe the same probabilities as 2 and 6. section 12.4
In C#, IDiscreteDistribution<T> exposes Support(), Weight(T), and Sample(). A uniform distribution over integers from 0 to max − 1 gives every supported integer weight 1. This separates reasoning about a distribution from individual calls to Random.Next. section 12.4
Listing 12.1 An interface and the discrete uniform distribution
public interface IDistribution<T>
{
T Sample();
}
public interface IDiscreteDistribution<T> : IDistribution<T>
{
IEnumerable<T> Support();
int Weight(T t);
}
public sealed class DiscreteUniform : IDiscreteDistribution<int>
{
private readonly Random rnd = new();
private readonly int max;
public static DiscreteUniform Distribution(int max)
{
if (max <= 0)
throw new InvalidOperationException();
return new(max);
}
private DiscreteUniform(int max) => this.max = max;
public int Sample() => rnd.Next(max);
public IEnumerable<int> Support() => Enumerable.Range(0, max);
public int Weight(int t) => 0 <= t && t < max ? 1 : 0;
}
The uniform implementation shows the key invariant directly: Weight returns 1 inside the support and 0 outside it. section 12.4
An unfair die made from a fair one
Project six equally weighted die outcomes onto colors: one green face, two red faces, and three blue faces. Grouping outcomes by color adds their weights, giving green:1, red:2, and blue:3. Dividing every weight by their greatest common divisor keeps an equivalent distribution with small integers. section 12.7
Integer weights avoid the rounding problem of probabilities stored as doubles that do not sum to exactly 1. Their trade-off appears when combining distributions: multiplying a prior weight by a conditional weight and dividing by that conditional distribution’s total can produce a fraction. To construct a joint distribution with integer weights, first scale by the least common multiple of the conditional total weights; each later division then comes out evenly. section 12.4 section 13.2
From prior weights to posterior weights
With prior weights 90 for no cold and 10 for a cold, conditional sneezing weights produce joint weights 27 for no cold and sneezing, and 86 for a cold and sneezing. Filtering to people who sneezed leaves posterior weights 27:86. These are relative strengths, not percentages. section 13.2 section 13.3
Weights also support combining distributions by merging equal outcomes and adding their weights. An empty distribution gives every value weight zero, but cannot produce a sample; joint-distribution calculations must exclude its zero total weight when computing the least common multiple. section 14.2



