Skip to content

Commit

Permalink
change uuid to use Random.RandomDevice() instead of Random.default_rn…
Browse files Browse the repository at this point in the history
…g() (#35872)

Co-authored-by: Rafael Fourquet <fourquet.rafael@gmail.com>
  • Loading branch information
ssikdar1 and rfourquet authored May 27, 2020
1 parent 806dfca commit 9559efc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Standard library changes
#### Distributed


#### UUIDs
* Change `uuid1` and `uuid4` to use `Random.RandomDevice()` as default random number generator ([#35872]).

Deprecated or removed
---------------------

Expand Down
26 changes: 22 additions & 4 deletions stdlib/UUIDs/src/UUIDs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions stdlib/UUIDs/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 9559efc

Please sign in to comment.