# Generating Float32 uniform random numbers with Distributions.jl

**URL:** <https://discourse.julialang.org/t/generating-float32-uniform-random-numbers-with-distributions-jl/63889>\
**Category:** General Usage\
**Tags:** distributions, random\
**Created:** [July 1, 2021, 1:53pm UTC](https://discourse.julialang.org/t/generating-float32-uniform-random-numbers-with-distributions-jl/63889 "2021-07-01T13:53:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bad\_at\_math](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bad\_at\_math](https://discourse.julialang.org/u/bad_at_math)\
**Post date:** [July 1, 2021, 1:53pm UTC](https://discourse.julialang.org/t/generating-float32-uniform-random-numbers-with-distributions-jl/63889/1 "2021-07-01T13:53:15Z")

</div>

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

```julia
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:

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

```

`y` is `Float32`.

Thank you!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 1, 2021, 2:39pm UTC](https://discourse.julialang.org/t/generating-float32-uniform-random-numbers-with-distributions-jl/63889/2 "2021-07-01T14:39:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)\
**Post date:** [November 6, 2023, 2:32pm UTC](https://discourse.julialang.org/t/generating-float32-uniform-random-numbers-with-distributions-jl/63889/3 "2023-11-06T14:32:43Z")

</div>

This still seems to be the behavior currently?
