Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What module or library do you use to generate universally unique identifier (UUID) in Erlang?

Tags:

uuid

erlang

What module or library do you use to generate universally unique identifier (UUID)?

like image 502
Kirill Trofimov Avatar asked Nov 01 '09 13:11

Kirill Trofimov


1 Answers

This code generate V4 UUID code

-module(uuid).
-export([uuid_v4/0]).
-define(VARIANT10, 2#10).
-define(UUIDv4, 4).


uuid_v4() ->
    <<U0:32, U1:16, _:4, U2:12, _:2, U3:30, U4:32>> = crypto:strong_rand_bytes(16),
    lists:flatten(io_lib:format("~8.16.0b-~4.16.0b-~4.16.0b-~2.16.0b~2.16.0b-~12.16.0b",get_binary_uuid(<<U0:32, U1:16, ?UUIDv4:4, U2:12, ?VARIANT10:2, U3:30, U4:32>>))).

get_binary_uuid(<<TL:32, TM:16, THV:16, CSR:8, CSL:8, N:48>>) ->
    [TL, TM, THV, CSR, CSL, N].

For compile run : c(uuid).

For run code : uuid:uuid_v4().

like image 189
suraj Avatar answered Sep 27 '22 22:09

suraj