# Uniuq no

**URL:** <https://www.ruby-forum.com/t/uniuq-no/160316>\
**Category:** Ruby\
**Created:** [February 14, 2009, 10:21am UTC](https://www.ruby-forum.com/t/uniuq-no/160316 "2009-02-14T10:21:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 10:21am UTC](https://www.ruby-forum.com/t/uniuq-no/160316/1 "2009-02-14T10:21:17Z")

</div>

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.I you know some other way ,please say  
to create unique id .

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 10:41am UTC](https://www.ruby-forum.com/t/uniuq-no/160316/2 "2009-02-14T10:41:50Z")

</div>

Vetrivel V. 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

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/uniuq-no/160316/3 "2009-02-14T16:21:14Z")

</div>

7stud – wrote:

> Vetrivel V. 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:  
[Bug in converting float to int? - Ruby - Ruby-Forum](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](http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/classes/BigDecimal.html)  
)

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 4:23pm UTC](https://www.ruby-forum.com/t/uniuq-no/160316/4 "2009-02-14T16:23:00Z")

</div>

Time.new.to\_i.to\_s\<\<rand.to\_s

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 14/02/2009, at 8:19 PM, Vetrivel V.

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 25, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/uniuq-no/160316/5 "2009-02-25T15:16:35Z")

</div>

- Vetrivel V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.I you know some other way ,please say  
> to create unique id .

UUID¹ is a standard way of constructing unique identifiers, and a Ruby  
library² exist already, so unless you require the identifiers to be  
integers, I suggest you use that.

[1] [Universally unique identifier - Wikipedia](http://en.wikipedia.org/wiki/UUID)  
[2] [http://wiki.github.com/assaf/uuid](http://wiki.github.com/assaf/uuid)

–  
Lars H.

“If anyone disagrees with anything I say, I am quite prepared not only  
to  
retract it, but also to deny under oath that I ever said it.” -Tom  
Lehrer

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 14, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/uniuq-no/160316/6 "2009-02-14T17:07:39Z")

</div>

Vetrivel V. 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.I you know some other way ,please say  
> to create unique id .

How about something like this…  
count = 0  
t = Time.now.to\_s  
10000.times {  
if( t == Time.now.to\_s)  
count = count + 1  
else  
count = 0  
t = Time.now.to\_s  
end  
uniqid = t + “.” + count.to\_s  
puts uniqid  
}

That way you don’t have to worry about rounding, or the possibility that  
the same random number might come up twice. The only real problem with  
this scheme is that on a very fast computer it might be possible to  
generate more ids each second than will fit in an Int. But, Bignum might  
be able to help there…

---

<div class="post-metadata">

**Author:** ![vetrivel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dc4da7/32.png) [@vetrivel](https://www.ruby-forum.com/u/vetrivel)\
**Post date:** [February 25, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/uniuq-no/160316/7 "2009-02-25T16:43:15Z")

</div>

On Feb 25, 2009, at 3:14 PM, Lars H. wrote:

> > to create unique id .
> 
> “If anyone disagrees with anything I say, I am quite prepared not  
> only to  
> retract it, but also to deny under oath that I ever said it.” -Tom  
> Lehrer

UUIDs can also be notated in an integer representation. It’s just a  
bit long.

There are multiple libraries, this example uses the gem uuidtools:

> > UUID.timestamp\_create.to\_i  
> > 227941543066369752189048382045440381927

They are the best way to choose.

Regards,  
Florian

–  
Florian G.

smtp: removed\_email\_address@domain.invalid  
jabber: removed\_email\_address@domain.invalid  
gpg: 533148E2
