If you want a linear drop-off what you're describing is called a triangle (or triangular) distribution. Given U
, a source of uniformly distributed random numbers on the range [0,1)
, you can generate a triangle on the range [a,b)
with its mode at a
using:
def triangle(a,b)
return a + (b-a)*(1 - sqrt(U))
end
This can be derived by writing the equation of a triangle for the specified range, scaling it so it has area 1 to make it a valid density, integrating to get the CDF, and using inversion.
As an interesting aside, this will still work if a >= b
. For equality, you always get a
(which makes sense if the range is zero). Otherwise, you get a triangle which goes from b
to a
and has its mode at a
.