How to create a random Uniform Distribution between (but excluding) 0 and 10?

Technically, the endpoints have a very small probability (theoretically 0, in practice \approx0 because of floating point). Depending on your application, you can reject and redraw.

Minimal example:

using Random

"`Uniform(0,b)`, with `0` excluded for sure, and we really mean it."
struct PositiveUniform{T}
    b::T
end

function Base.rand(rng::Random.AbstractRNG, pu::PositiveUniform)
    while true
        r = rand(rng)
        r > 0 && return r * pu.b
    end
end

rand(PositiveUniform(10))

I am curious why you expected it to work. Adding a closing ),

julia> rand(Uniform(>0, 10))
ERROR: syntax: ">" is not a unary operator

informs you that it fails at parsing >0. You are dealing with a programming language, and can’t just make up arbitrary expressions and hope it will do something sensible.

12 Likes