Generating Float32 uniform random numbers with Distributions.jl

I can define and sample from a uniform distribution with Float32 bounds as follows:

using Distributions

dist = Uniform{Float32}(-1.0f0, 1.0f0)
x = rand(dist)

However, the x here has type Float64, not Float32. Is there a way to force rand(dist) to return a Float32, other than casting it after the fact?

This behavior seems kind of inconsistent across Distributions.jl. In the following example:

dist = Normal{Float32}(0.1f0, 1.0f0)
y = rand(dist)

y is Float32.

Thank you!

1 Like

This is a bug. I’ve made Fix `rand(Uniform{Float32})` by oscardssmith · Pull Request #1355 · JuliaStats/Distributions.jl · GitHub to fix it.

3 Likes