What module/library do you use to generate uuid?
问题:
回答1:
For future googlers like myself, erlang-uuid from avtobiff works very simply.
回答2:
from http://github.com/travis/erlang-uuid
-module(uuid).
-export([v4/0, to_string/1, get_parts/1]).
-import(random).
v4() ->
v4(random:uniform(math:pow(2, 48)) - 1, random:uniform(math:pow(2, 12)) - 1, random:uniform(math:pow(2, 32)) - 1, random:uniform(math:pow(2, 30)) - 1).
v4(R1, R2, R3, R4) ->
<<R1:48, 4:4, R2:12, 2:2, R3:32, R4: 30>>.
to_string(U) ->
lists:flatten(io_lib:format("~8.16.0b-~4.16.0b-~4.16.0b-~2.16.0b~2.16.0b-~12.16.0b", get_parts(U))).
get_parts(<<TL:32, TM:16, THV:16, CSR:8, CSL:8, N:48>>) ->
[TL, TM, THV, CSR, CSL, N].
回答3:
Uuid generator from couchdb: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_uuids.erl
回答4:
I recommend using the ossp-uuid nif bindings for Erlang that has rebar support https://github.com/yrashk/erlang-ossp-uuid
ossp_uuid:make(v4, text)
回答5:
Why did you use round(math:pow(2, 48))
? I think that 1 bsl 48
will work more quickly and code will not lose understanding.
回答6:
Try this one: https://github.com/afiskon/erlang-uuid-v4 The simplest implementation ever.
回答7:
If you don't need follow RFC 4122 you can use now/0
call to generate unique ID without external dependencies, because tuple, generated by now call is absolutely unique inside VM and unique with large probability beetween nodes.
回答8:
The one I wrote as an example of style and documentation -- based on the zillion recommendations I received from the kind-enough-to-be-mean folks on the Erlang list.
Library: https://gitlab.com/zxq9/zuuid
Docs: http://zxq9.com/projects/zuuid/docs/
PS: Huge thanks to the awesome folks of erlang-questions for taking the time to sharpshoot me on everything. The lib is far better for it.