Skip to content

Base.rand() produces too many collisions for Float32s #40355

@simsurace

Description

@simsurace

Minimal working example (Tested on Julia 1.5 and 1.6.)

julia> A = rand(Float32, 2^20);
julia> length(A)-length(unique(A))

Expected behavior
The expected number of duplicates when drawing n times at random from a set of N elements with replacement is equal to (see e.g. Proposition 4.1 in this paper)

expected_number_of_duplicates(N, n) = n - N * (1 - (1 - 1/N)^n)

For drawing a uniform Float32, we therefore expect

julia> expected_number_of_duplicates(2^32, 2^20)
127.98946189880371

Actual behavior
I obtain numbers that are on the order of 60000.

Metadata

Metadata

Assignees

No one assigned

    Labels

    randomnessRandom number generation and the Random stdlib

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions