[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

uniuq no

Vetrivel Vetrivel

2/14/2009 9:20:00 AM

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 .
--
Posted via http://www.ruby-....

6 Answers

7stud --

2/14/2009 9:40:00 AM

0

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

--
Posted via http://www.ruby-....

Raphael Clancy

2/14/2009 3:20:00 PM

0

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-...to... ) 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/BigDe...
)
--
Posted via http://www.ruby-....

Julian Leviston

2/14/2009 3:21:00 PM

0

Time.new.to_i.to_s<<rand.to_s

Blog: http://random8.ze...
Learn rails: http://sensei.ze...

On 14/02/2009, at 8:19 PM, Vetrivel Vetrivel
<vetrivel.bksys@gmail.com> 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 .
> --
> Posted via http://www.ruby-....
>

Raphael Clancy

2/14/2009 4:06:00 PM

0

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.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...
--
Posted via http://www.ruby-....

Lars Haugseth

2/25/2009 1:54:00 PM

0

* Vetrivel Vetrivel <vetrivel.bksys@gmail.com> 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] http://en.wikipedia.org...
[2] http://wiki.github.com/...

--
Lars Haugseth

"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

Florian Gilcher

2/25/2009 3:42:00 PM

0


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

> * Vetrivel Vetrivel <vetrivel.bksys@gmail.com> 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 =3D Time.new
>> p "%10.5f" % t.to_f
>>
>> does it give unique value always.I you know some other way ,please =20=

>> say
>> to create unique id .
>
> UUID=B9 is a standard way of constructing unique identifiers, and a =
Ruby
> library=B2 exist already, so unless you require the identifiers to be
> integers, I suggest you use that.
>
> [1] http://en.wikipedia.org...
> [2] http://wiki.github.com/...
>
> --=20
> Lars Haugseth
>
> "If anyone disagrees with anything I say, I am quite prepared not =20
> only to
> retract it, but also to deny under oath that I ever said it." -Tom =20
> Lehrer
>

UUIDs can also be notated in an integer representation. It's just a =20
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 Gilcher

smtp: flo@andersground.net
jabber: Skade@jabber.ccc.de
gpg: 533148E2