20

What's a canonical way to generate UUIDs in Elixir? Should I necessarily use the library https://hex.pm/packages/uuid or is there a built-in library? I better have less dependencies and do more work than vise versa, therefore if I can generate in Elixir with an external dependency, it'll better go with it.

9
  • 2
    Why do you need to produce UUIDs? There are simpler built-in ways to generate unique keys/identifiers for things if you don't actually require true UUIDs.
    – typesend
    May 20, 2017 at 6:08
  • @BenDamman, what are they?
    – Jodimoro
    May 20, 2017 at 6:47
  • 2
    :rand.uniform(), for example. Integer.to_string(:rand.uniform(4294967296), 32) <> Integer.to_string(:rand.uniform(4294967296), 32) will give you a 64 bit random identifier in base64 (don't get more than 32 bits or so out of rand.uniform - so if you need the full 128 bits, do four calls and string them together)
    – cdegroot
    May 20, 2017 at 16:54
  • (no clue why I can't edit my own comment, but here's a 128 bit version that should work well: ((:rand.uniform(4294967296) <<< 96) ||| (:rand.uniform(4294967296) <<< 64) ||| (:rand.uniform(4294967296) <<< 32) ||| (:rand.uniform(4294967296))) |> Integer.to_string(36) - after a use Bitwise, of course)
    – cdegroot
    May 20, 2017 at 17:02
  • @cdegroot, thx.
    – Jodimoro
    May 20, 2017 at 17:48

4 Answers 4

37
import Ecto

uuid = Ecto.UUID.generate
3
  • 12
    Not every Elixir project uses DB and Ecto mate.
    – Kociamber
    Jun 22, 2019 at 9:34
  • You don't need to import Ecto, do you? Ecto.UUID.generate works by itself (assuming Ecto is present as a dependency.)
    – GMA
    Aug 26, 2022 at 15:04
  • If you really do not want to have dependencies, you could just take the parts you need from Ecto's source code. If all you need is generate, it would be a matter of copying the 4 functions at the bottom of this file: github.com/elixir-ecto/ecto/blob/master/lib/ecto/uuid.ex Apr 3, 2023 at 4:06
15

If you're using elixir with ecto, you can always use Ecto.UUID https://hexdocs.pm/ecto/Ecto.UUID.html

9

The canonical way to generate a globally unique reference in Elixir is with make_ref/0.

From the documentation:

Returns an almost unique reference.

The returned reference will re-occur after approximately 2^82 calls; therefore it is unique enough for practical purposes.

1
  • I wonder what happens if the OTP restarts. Apr 3, 2023 at 23:20
2

If you don't want to include Ecto in your project, you should evaluate https://github.com/zyro/elixir-uuid

defp deps do
  [ { :elixir_uuid, "~> 1.2" } ]
end
iex> UUID.uuid1()
"5976423a-ee35-11e3-8569-14109ff1a304"
iex> UUID.uuid3(:dns, "my.domain.com")
"03bf0706-b7e9-33b8-aee5-c6142a816478"

iex> UUID.uuid3("5976423a-ee35-11e3-8569-14109ff1a304", "my.domain.com")
"0609d667-944c-3c2d-9d09-18af5c58c8fb"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.