# Repeatable generation of (fake) UUIDs

**URL:** <https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 17, 2019, 11:13am UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960 "2019-03-17T11:13:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 17, 2019, 11:13am UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960/1 "2019-03-17T11:13:07Z")

</div>

I need to generate something that would look like a UUID (so that `UUID(something)` will not error), but depend on some input.

So in the same way this works:

```julia
rand(MersenneTwister(0)) == rand(MersenneTwister(0))

```

I want this to work:

```julia
uuid1(MersenneTwister(0)) == uuid1(MersenneTwister(0))

```

I understand that this behavior defeats the purpose of a UUID, but I need to cheat the system 😉

Sadly, none of these attempts work:

```julia
# I understand why this doesn't work: 
# rand doesn't know which version
# UUID this is supposed to be
rand(MersenneTwister(0), UUID)  

# not sure what the correct format
# for a version 1 UUID is. Apparently
# not this... 
UUID(join([join(rand(['a':'z'; 0:9], n)) for n in ns], '-')) is...

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 17, 2019, 11:27am UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960/2 "2019-03-17T11:27:09Z")

</div>

The magic of posting a question here is it always gets answered. Regardless by whom:

```julia
function fakeuuid(rng::AbstractRNG)
    ns = [4, 2, 2, 2, 6]
    UUID(join([bytes2hex(rand(rng, UInt8, n)) for n in ns], '-'))
end
fakeuuid(MersenneTwister(0)) == fakeuuid(MersenneTwister(0))

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [March 17, 2019, 11:44am UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960/3 "2019-03-17T11:44:15Z")

</div>

You can also use `uuid4`:

```julia
julia> rng = MersenneTwister(0); uuid4(rng)
UUID("bfad144b-f725-4b28-a1be-0e591a3b69ea")

julia> rng = MersenneTwister(0); uuid4(rng)
UUID("bfad144b-f725-4b28-a1be-0e591a3b69ea")

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 17, 2019, 1:22pm UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960/4 "2019-03-17T13:22:23Z")

</div>

Hmmf, that would be ideal, but it doesn’t work for me?

```julia
julia> rng = MersenneTwister(0)
MersenneTwister(UInt32[0x00000000], Random.DSFMT.DSFMT_state(Int32[748398797, 1073523691, -1738140313, 1073664641, -1492392947, 1073490074, -1625281839, 1073254801, 1875112882, 1073717145 … 943540191, 1073626624, 1091647724, 1073372234, -1273625233, -823628301, 835224507, 991807863, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000 … 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)

julia> u1 = uuid4(rng) 
UUID("bfad144b-f725-4b28-a1be-0e591a3b69ea")

julia> u2 = uuid4(rng)
UUID("79ed9db9-ec79-46a0-99c5-f638a776ab3c")

julia> u1 == u2
false

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 17, 2019, 1:31pm UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960/5 "2019-03-17T13:31:05Z")

</div>

Same reason why

```julia
julia> rng = MersenneTwister(0);

julia> rand(rng)
0.8236475079774124

julia> rand(rng)
0.9103565379264364

```

gives you two number. Given the same seed `uuid4` produces the same number.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 17, 2019, 1:32pm UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960/6 "2019-03-17T13:32:35Z")

</div>

Oh lord. Sorry. Of course … 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 17, 2019, 1:53pm UTC](https://discourse.julialang.org/t/repeatable-generation-of-fake-uuids/21960/7 "2019-03-17T13:53:58Z")

</div>

Of course, it is easy to fix some other way:

```julia
import Random

struct ConstantRNG{T} <: Random.AbstractRNG
    x::T # it _is_ random. just doesn't change
end

Random.rand(rng::ConstantRNG, ::Type{T}) where T = T(rng.x)

rng = ConstantRNG(1)

```

then

```julia
julia> rand(rng)
1.0

julia> rand(rng)
1.0

```

😉
