tags:

views:

338

answers:

5

I tried:

os:cmd("uuidgen -t")

: but this is platform dependant and returns a tuple. I just want a binary value which I can use.

I want something based on MAC address, 128bit, and where I can just include a module and use module:uuid() to get a string hex UUID, or module:binary_uuid, something simple like that. So far it doesn't seem to exist in Erlang.

Update:

Noone could give a correct answer so in the end I used:

hex_uuid() -> os:cmd("uuidgen").

It won't work on Windows, but works on Linux and Mac

+3  A: 

Take a look here: http://github.com/travis/erlang-uuid

filippo
Is there a BIF or a standard Erlang library to do this?
Zubair
Have you used this library as I couldn't get it to work. <pre>uuid:v4().** exception error: no function clause matching random:uniform(281474976710656.0) in function uuid:v4/0</pre>
Zubair
Hi I've used it in the past. Worked for me with erlang R12B, but on R13B I'm getting the same error. On github there are some forks. This one http://github.com/akreiling/erlang-uuid work on my machine
filippo
this lib is easily fixed by changing math:pow(..) to erlang:trunc(math:pow(..))random:uniform no longer takes floats (or math:pow now returns floats)
I think I'll pass on that, I'm looking for something that is battle tested and doesn't need patching. Thanks anyway
Zubair
+4  A: 

You can also check out how CouchDB generates UUIDs. They use 3 different methods of generating it according to taste. random, utc_random and sequential.

http://svn.apache.org/repos/asf/couchdb/trunk/src/couchdb/couch_uuids.erl

Jon Gretar
I was hoping not to have to look at lots of source code. Its ok, a bit of a -1 for Erlang not including it in their standard libraries though
Zubair
which one do I use if I want a truly unique UUID.
Zubair
+1  A: 

http://stackoverflow.com/questions/1657204/erlang-uuid-generator

sinnus
As I said in another answer I already tried the UUID generator they describe in that question, and it doesn't work.
Zubair
http://svn.apache.org/repos/asf/couchdb/trunk/src/couchdb/couch_uuids.erl works
sinnus
Do I need to install couchdb to use it as I saw the following in the code?"-include("couch_db.hrl")."
Zubair
http://svn.apache.org/repos/asf/couchdb/trunk/src/couchdb/couch_db.hrl
sinnus
How are they generated, MAC and 128 bit?
Zubair
It depends on crypto module
sinnus
+1  A: 

You'll find some functions in my lib at http://code.google.com/p/tideland-cel/source/browse/trunk/src/celutl.erl. I realized it as version 4.

Mue
I just looked at the code but some things I wasn't sure about. How many bits is the UUID and how does it generate the uniqueness as I saw no reference to a MAC address.
Zubair
Sorry for answering so late. There are several versions of UUIDs, see http://en.wikipedia.org/wiki/Universally_Unique_Identifier. I've just choosen v4 where it's based on random. The chance of duplicates is really low, especially in one context (set of systems generating and exchanging UUIDs).
Mue
+1  A: 

hex_uuid() -> os:cmd("uuidgen").

It won't work on Windows, but works on Linux and Mac

Zubair