Announcement (2017-05-07): www.ruby-forum.com is now read-only since I
unfortunately do not have the time to support and maintain the forum any
more. Please see rubyonrails.org/community and ruby-lang.org/en/community
for other Rails- und Ruby-related community platforms.

Anyone have a fast way of generating a verson 4 UUID?
I have found some libraries to do this, but it seems like it could be
done in about 8 lines of code. Any faster approches?
what about this?
def rand_hex(l)
validChars = ("A".."F").to_a + ("0".."9").to_a
length = validChars.size
hexCode = ""
1.upto(l) do |i|
hexCode << validChars[rand(length-1)]
end
hexCode
end
r =
"#{rand_hex(8)}-#{rand_hex(4)}-#{rand_hex(4)}-#{rand_hex(4)}-#{rand_hex(12)}"
puts r
thanks

On 08.03.2007 22:44, Aaron Smith wrote:
> hexCode = ""> 1.upto(l) do |i|> hexCode << validChars[rand(length-1)]> end> hexCode> end>> r => "#{rand_hex(8)}-#{rand_hex(4)}-#{rand_hex(4)}-#{rand_hex(4)}-#{rand_hex(12)}"> puts r
Make validChars and length constants declared outside the method. And
also, use rand(length) otherwise you never get a "9". You could also
use ?A..?F and ?0..?9 - no idea whether that makes a performance diff.
But I guess this one is even faster:
def rand_hex_2(l)
rand(1 << (l*4)).to_a(16)
end
Kind regards
robert

On Mar 11, 2007, at 6:00 PM, Ryan Davis wrote:
> Yes, 21 uuids per second!>> I got a 400x improvement out of it by dropping this into our config/> environment.rb:>> def UUID.true_random> (1..8).to_a.map { rand(0x10000) }.pack("n8")> end
I couldn't tell from the message if Ryan or Aaron wrote the
parts I quoted above...
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
Gary Wright

On Mar 11, 2007, at 5:16 PM, Gary Wright wrote:
> I couldn't tell from the message if Ryan or Aaron wrote the> parts I quoted above...
I wrote all those parts.
> 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.
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.

On Mar 11, 6:04 pm, Ryan Davis <ryand-r...@zenspider.com> wrote:
> 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

You might want to consider renaming them in your project so that
someone else that inherits your code won't think that they're really
universally unique. As it stands right now, the methods above will
definitely allow collisions, and will cause headaches (and possible
data loss...) if someone uses them with that assumption in mind.