From: Aaron Smith Date: 2007-03-09T07:17:48+09:00 Subject: Re: generate UUID Robert Klemme wrote: > 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 def rand_hex_2(l) rand (1 << (l*4) ).to_a(16) end puts rand_hex_2(9) ArgumentError: wrong number of arguments (1 for 0) -- Posted via http://www.ruby-forum.com/.