diff --git a/NEWS.md b/NEWS.md index 9205c406092b1..a19fa40e44f82 100644 --- a/NEWS.md +++ b/NEWS.md @@ -66,6 +66,9 @@ Standard library changes #### Distributed +#### UUIDs +* Change `uuid1` and `uuid4` to use `Random.RandomDevice()` as default random number generator ([#35872]). + Deprecated or removed --------------------- diff --git a/stdlib/UUIDs/src/UUIDs.jl b/stdlib/UUIDs/src/UUIDs.jl index ee2064605c7c8..7fb83a393be00 100644 --- a/stdlib/UUIDs/src/UUIDs.jl +++ b/stdlib/UUIDs/src/UUIDs.jl @@ -36,12 +36,21 @@ const namespace_oid = UUID(0x6ba7b8129dad11d180b400c04fd430c8) # 6ba7b812-9dad- const namespace_x500 = UUID(0x6ba7b8149dad11d180b400c04fd430c8) # 6ba7b814-9dad-11d1-80b4-00c04fd430c8 """ - uuid1([rng::AbstractRNG=GLOBAL_RNG]) -> UUID + uuid1([rng::AbstractRNG]) -> UUID Generates a version 1 (time-based) universally unique identifier (UUID), as specified by RFC 4122. Note that the Node ID is randomly generated (does not identify the host) according to section 4.5 of the RFC. +The default rng used by `uuid1` is not `GLOBAL_RNG` and every invocation of `uuid1()` without +an argument should be expected to return a unique identifier. Importantly, the outputs of +`uuid1` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6), +`uuid1` uses `Random.RandomDevice` as the default rng. However, this is an implementation +detail that may change in the future. + +!!! compat "Julia 1.6" + The output of `uuid1` does not depend on `GLOBAL_RNG` as of Julia 1.6. + # Examples ```jldoctest; filter = r"[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}" julia> rng = MersenneTwister(1234); @@ -50,7 +59,7 @@ julia> uuid1(rng) UUID("cfc395e8-590f-11e8-1f13-43a2532b2fa8") ``` """ -function uuid1(rng::AbstractRNG=Random.default_rng()) +function uuid1(rng::AbstractRNG=Random.RandomDevice()) u = rand(rng, UInt128) # mask off clock sequence and node @@ -74,11 +83,20 @@ function uuid1(rng::AbstractRNG=Random.default_rng()) end """ - uuid4([rng::AbstractRNG=GLOBAL_RNG]) -> UUID + uuid4([rng::AbstractRNG]) -> UUID Generates a version 4 (random or pseudo-random) universally unique identifier (UUID), as specified by RFC 4122. +The default rng used by `uuid4` is not `GLOBAL_RNG` and every invocation of `uuid4()` without +an argument should be expected to return a unique identifier. Importantly, the outputs of +`uuid4` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6), +`uuid4` uses `Random.RandomDevice` as the default rng. However, this is an implementation +detail that may change in the future. + +!!! compat "Julia 1.6" + The output of `uuid4` does not depend on `GLOBAL_RNG` as of Julia 1.6. + # Examples ```jldoctest julia> rng = MersenneTwister(1234); @@ -87,7 +105,7 @@ julia> uuid4(rng) UUID("196f2941-2d58-45ba-9f13-43a2532b2fa8") ``` """ -function uuid4(rng::AbstractRNG=Random.default_rng()) +function uuid4(rng::AbstractRNG=Random.RandomDevice()) u = rand(rng, UInt128) u &= 0xffffffffffff0fff3fffffffffffffff u |= 0x00000000000040008000000000000000 diff --git a/stdlib/UUIDs/test/runtests.jl b/stdlib/UUIDs/test/runtests.jl index 8258ac3f27d68..483b7f1d56938 100644 --- a/stdlib/UUIDs/test/runtests.jl +++ b/stdlib/UUIDs/test/runtests.jl @@ -54,3 +54,11 @@ for (init_uuid, next_uuid) in standard_namespace_uuids result = uuid5(init_uuid, "julia") @test next_uuid == result end + +# Issue 35860 +Random.seed!(Random.GLOBAL_RNG, 10) +u1 = uuid1() +u4 = uuid4() +Random.seed!(Random.GLOBAL_RNG, 10) +@test u1 != uuid1() +@test u4 != uuid4()