From: Assaf Date: 2007-03-13T03:00:12+09:00 Subject: Re: generate UUID On Mar 11, 6:04 pm, Ryan Davis wrote: > > I'm not an expert on UUIDs but I think there is a real > > semantic difference between a UUID and a random number > > of the same (binary) length. RFC 4122 has lots of good > > information on UUIDs:http://www.ietf.org/rfc/rfc4122.txt > > No, you're right, but for the most part, it really doesn't matter for > any of my company's uses. The most popular UUID format I've seen uses > the mac address as part of the encoding. I don't know of any > portable way for a ruby script to get that, and with all our UUIDs > being generated from a single server for the foreseeable future... > yeah. Not too useful. A random number will work fine if you want something that looks like a UUID. But if you want a Universally Unique Identifier, the time-based UUID works better. It uses the clock time and a restart sequence to gaurantee unique identifiers for the same machine, combined with the Mac address to guarantee universal uniqueness (aka unique in space and time). On Windows you can use ipconfig to get the MAC address, and on Linux/ BSD/OS/X use ifconfig. There's no portable way, but you can run both and see which answer you get. Which is exactly what this UUID library does (disclosure: I'm the author): http://rubyforge.org/projects/reliable-msg These are the numbers I get for generating 1 million UUIDs on a Duo Core 2. Hopefully they're fast enough for you: > cat test.rb require "lib/uuid.rb" 1000000.times { UUID.new } > time ruby test.rb uuid: Initialized UUID generator with sequence number 0x6832 and MAC address 00:13:A9:8A:FA:AA real 0m27.983s user 0m18.796s sys 0m9.051s Assaf http://labnotes.org > > In the specific case of uuidtools, it actually goes through WAY more > convolutions that ever necessary and (according to the linux manpage > on /dev/urandom) actually REDUCES the randomness of the UUIDs that it > generates. I just couldn't live with my test times anymore. 30% was > being spent on generating UUIDs that weren't any better than my > random blob. Remember, 21 uuids per second. *blech* > > I mostly wrote the rest of the code to illustrate additional methods > to get more performance out of (pure) ruby.