# Are rand(Float32) really uniform in \[0,1)?

**URL:** https://discourse.julialang.org/t/are-rand-float32-really-uniform-in-0-1/133585
**Category:** General Usage
**Tags:** question
**Created:** [October 31, 2025, 11:03pm UTC](https://discourse.julialang.org/t/are-rand-float32-really-uniform-in-0-1/133585 "2025-10-31T23:03:53Z")
**Posts on this page:** 1
**Showing post:** 20

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [November 3, 2025, 6:51pm UTC](https://discourse.julialang.org/t/are-rand-float32-really-uniform-in-0-1/133585/20 "2025-11-03T18:51:46Z")

</div>

> [@FattiMei](#):
>
> What is the metric to compare all the generation schemes proposed so far?

First you need to define what a uniform distribution _means_ in the first place. The best formal meaning I have for our current definition is:

`rand(Float32) < p` has probability p of being true for all p that are multiples of 2^{-24} in [0, 1]. Note that the Float32s in [0.5, 1] _are_ the multiples of 2^{-24}. The exact inequality operator here matters, as does the precision of p. It matters _how you use it!_

> [@Output distribution of rand(Float32) and rand(Float64), thread 2](https://discourse.julialang.org/t/output-distribution-of-rand-float32-and-rand-float64-thread-2/105184/143#interlude-are-we-checking-the-uniformity-of-the-distribution-the-right-way-1):
>
> This is a fun problem. Here’s my crack at it: function randf( ::Type{T}, u::UInt64 = rand(UInt64), ) where {T\<:Union{Float16,Float32,Float64}} # only depends on T s = 8\*sizeof(T) p = Base.\_precision(T) - 1 U = T === Float16 ? UInt16 : T === Float32 ? UInt32 : T === Float64 ? UInt64 : error() m = T === Float16 ? one(T) : inv(reinterpret(T, U(65 \<\< p))) # real computation z = leading\_zeros(u) if T !== Float16 b = (u \<\< (z + …

> [@Output distribution of rand(Float32) and rand(Float64), thread 2](https://discourse.julialang.org/t/output-distribution-of-rand-float32-and-rand-float64-thread-2/105184/147):
>
> So on the [0,1) vs (0,1] thing, it has everything to do with how you interpret and work with the results of rand() — and I finally have a good mental model here. Forget binary for a moment; let’s pretend our datatype of interest is decimal-based float with two digits and has a random function defined as randpct() = rand(0:99)/100. Given that definition, how do I implement a test that returns true 5% of the time? I know I just instinctively reach for randpct() \< 0.05. But why \<? And why not \<=…

---

_[View the full topic](https://discourse.julialang.org/t/are-rand-float32-really-uniform-in-0-1/133585)._
