= Solution
Use a widened interval width, so the arithmetic also handles endpoints near the limits of `Int`:
``
final class Uniform(l: Int, u: Int) extends FiniteDist[Int] {
require(l <= u)
private val width = u.toLong - l.toLong + 1L
def getDom: List[Int] = {
val builder = List.newBuilder[Int]
var value = l.toLong
while (value <= u.toLong) {
builder += value.toInt
value += 1L
}
builder.result()
}
def getProb(x: Int): Double =
if (l <= x && x <= u) 1.0 / width.toDouble else 0.0
}
``
The support <lists> every <integer> in the inclusive interval once. Its <cardinality> is `width`, so assigning each atom mass $1/width$ gives total mass one. The loop counter is also widened, preventing an increment past `Int.MaxValue` from wrapping to a negative value. Returning the full support takes time and space proportional to its width and can be impractical for a very large interval; the mass query itself is constant-time. The full <integer> interval has width $2^{32}$, which fits `Long` but not positive `Int`.
Back to article page