Skip to content

Commit 9559efc

Browse files
ssikdar1rfourquet
andauthored
change uuid to use Random.RandomDevice() instead of Random.default_rng() (JuliaLang#35872)
Co-authored-by: Rafael Fourquet <fourquet.rafael@gmail.com>
1 parent 806dfca commit 9559efc

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ Standard library changes
7474
#### Distributed
7575

7676

77+
#### UUIDs
78+
* Change `uuid1` and `uuid4` to use `Random.RandomDevice()` as default random number generator ([#35872]).
79+
7780
Deprecated or removed
7881
---------------------
7982

stdlib/UUIDs/src/UUIDs.jl

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,21 @@ const namespace_oid = UUID(0x6ba7b8129dad11d180b400c04fd430c8) # 6ba7b812-9dad-
3636
const namespace_x500 = UUID(0x6ba7b8149dad11d180b400c04fd430c8) # 6ba7b814-9dad-11d1-80b4-00c04fd430c8
3737

3838
"""
39-
uuid1([rng::AbstractRNG=GLOBAL_RNG]) -> UUID
39+
uuid1([rng::AbstractRNG]) -> UUID
4040
4141
Generates a version 1 (time-based) universally unique identifier (UUID), as specified
4242
by RFC 4122. Note that the Node ID is randomly generated (does not identify the host)
4343
according to section 4.5 of the RFC.
4444
45+
The default rng used by `uuid1` is not `GLOBAL_RNG` and every invocation of `uuid1()` without
46+
an argument should be expected to return a unique identifier. Importantly, the outputs of
47+
`uuid1` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6),
48+
`uuid1` uses `Random.RandomDevice` as the default rng. However, this is an implementation
49+
detail that may change in the future.
50+
51+
!!! compat "Julia 1.6"
52+
The output of `uuid1` does not depend on `GLOBAL_RNG` as of Julia 1.6.
53+
4554
# Examples
4655
```jldoctest; filter = r"[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}"
4756
julia> rng = MersenneTwister(1234);
@@ -50,7 +59,7 @@ julia> uuid1(rng)
5059
UUID("cfc395e8-590f-11e8-1f13-43a2532b2fa8")
5160
```
5261
"""
53-
function uuid1(rng::AbstractRNG=Random.default_rng())
62+
function uuid1(rng::AbstractRNG=Random.RandomDevice())
5463
u = rand(rng, UInt128)
5564

5665
# mask off clock sequence and node
@@ -74,11 +83,20 @@ function uuid1(rng::AbstractRNG=Random.default_rng())
7483
end
7584

7685
"""
77-
uuid4([rng::AbstractRNG=GLOBAL_RNG]) -> UUID
86+
uuid4([rng::AbstractRNG]) -> UUID
7887
7988
Generates a version 4 (random or pseudo-random) universally unique identifier (UUID),
8089
as specified by RFC 4122.
8190
91+
The default rng used by `uuid4` is not `GLOBAL_RNG` and every invocation of `uuid4()` without
92+
an argument should be expected to return a unique identifier. Importantly, the outputs of
93+
`uuid4` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6),
94+
`uuid4` uses `Random.RandomDevice` as the default rng. However, this is an implementation
95+
detail that may change in the future.
96+
97+
!!! compat "Julia 1.6"
98+
The output of `uuid4` does not depend on `GLOBAL_RNG` as of Julia 1.6.
99+
82100
# Examples
83101
```jldoctest
84102
julia> rng = MersenneTwister(1234);
@@ -87,7 +105,7 @@ julia> uuid4(rng)
87105
UUID("196f2941-2d58-45ba-9f13-43a2532b2fa8")
88106
```
89107
"""
90-
function uuid4(rng::AbstractRNG=Random.default_rng())
108+
function uuid4(rng::AbstractRNG=Random.RandomDevice())
91109
u = rand(rng, UInt128)
92110
u &= 0xffffffffffff0fff3fffffffffffffff
93111
u |= 0x00000000000040008000000000000000

stdlib/UUIDs/test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ for (init_uuid, next_uuid) in standard_namespace_uuids
5454
result = uuid5(init_uuid, "julia")
5555
@test next_uuid == result
5656
end
57+
58+
# Issue 35860
59+
Random.seed!(Random.GLOBAL_RNG, 10)
60+
u1 = uuid1()
61+
u4 = uuid4()
62+
Random.seed!(Random.GLOBAL_RNG, 10)
63+
@test u1 != uuid1()
64+
@test u4 != uuid4()

0 commit comments

Comments
 (0)