From: Raphael Clancy Date: 2009-02-15T00:19:40+09:00 Subject: Re: uniuq no 7stud -- wrote: > Vetrivel Vetrivel wrote: >> I want to create new unique id in ruby.I have used the following >> coding.But I don''t know whether it works perfectly or not. >> >> t = Time.new >> p "%10.5f" % t.to_f >> >> does it give unique value always. > > This might be better: > > d = Time.new > t = d.to_f > puts t > > r = rand > puts r > > p_id = $$ > puts p_id > > new_id = t + 10000000 * r + 10000000 * p_id > p new_id Be a little careful with this, floats are not very precise, (see: http://www.ruby-forum.com/topic/178503 ) and this could lead to some duplication. irb(main):001:0> a = (10.12 * 100).to_i => 1011 irb(main):002:0> b = (10.11 * 100).to_i => 1011 irb(main):003:0> a == b => true While this really shouldn't be a problem with this scheme, big decimal might be safer... (see: http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/classes/BigDecimal.html ) -- Posted via http://www.ruby-forum.com/.