473,811 Members | 3,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use system time to generate unique id?

I need to generate unique id for my application. Use system time to create
id, which is the best fit, but I don't know how to do it. Please help.

Thanks in advance for any input.
Nov 21 '05
28 17698
Nina,
Yes. Considering the entire GUID is what is guaranteed on being unique.

GUID's are not sequential, they are seemingly random numbers, based on a
complicated formula that ensures uniqueness, so I'm not sure where or how
Marina is thinking you can truncate it.

Although GUID's are numbers I do not consider them numbers in the same sense
as Integer, Double, or Decimal, as they are actually an identifier, a
Globally Unique IDentifier. Some articles use the term universally unique
identifier.

Hope this helps
Jay

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:03******** *************** ***********@mic rosoft.com...
Thanks. Will truncate causing duplication?

"Marina" wrote:
There is nothing stopping you from truncating it.

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:D2******** *************** ***********@mic rosoft.com...
> Thanks again. The Id that generated by GUID is very long. Is there
> any

way
> that I can generate a unique id with a specific length?
>
> "Herfried K. Wagner [MVP]" wrote:
>
> > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
> > > Thank you all for ideas and suggestions. Please tell me how does
> > > GUID
> > > work?
> > > In case, user exits the application, and lunches the application
> > > again
> > > later,
> > > could GUID generate same id that was generated in previous session?

If
> > > this
> > > could happen then I cannot use it.
> >
> > That's very, very, very, ..., very unlikely. In practice it's

"guaranteed "
> > that every GUID only occurs once. There are various parameters used
> > to
> > calculate the GUID. So you can use a GUID as ID without any fear
> > that
> > you'll get a GUID twice.
> >
> > --
> > M S Herfried K. Wagner
> > M V P <URL:http://dotnet.mvps.org/>
> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
> >
> >
> >


Nov 21 '05 #11
Since the whole GUID is completely random and unique, a subset of it should
be pretty random as well. Obviously, as you have less characters in a
string, the odds of getting repeats increases. However, if you still have 16
characters, the odds of this happening are still pretty low.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e3******** ******@TK2MSFTN GP09.phx.gbl...
Nina,
Yes. Considering the entire GUID is what is guaranteed on being unique.

GUID's are not sequential, they are seemingly random numbers, based on a
complicated formula that ensures uniqueness, so I'm not sure where or how
Marina is thinking you can truncate it.

Although GUID's are numbers I do not consider them numbers in the same sense as Integer, Double, or Decimal, as they are actually an identifier, a
Globally Unique IDentifier. Some articles use the term universally unique
identifier.

Hope this helps
Jay

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:03******** *************** ***********@mic rosoft.com...
Thanks. Will truncate causing duplication?

"Marina" wrote:
There is nothing stopping you from truncating it.

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:D2******** *************** ***********@mic rosoft.com...
> Thanks again. The Id that generated by GUID is very long. Is there
> any
way
> that I can generate a unique id with a specific length?
>
> "Herfried K. Wagner [MVP]" wrote:
>
> > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
> > > Thank you all for ideas and suggestions. Please tell me how does
> > > GUID
> > > work?
> > > In case, user exits the application, and lunches the application
> > > again
> > > later,
> > > could GUID generate same id that was generated in previous session? If
> > > this
> > > could happen then I cannot use it.
> >
> > That's very, very, very, ..., very unlikely. In practice it's
"guaranteed "
> > that every GUID only occurs once. There are various parameters used > > to
> > calculate the GUID. So you can use a GUID as ID without any fear
> > that
> > you'll get a GUID twice.
> >
> > --
> > M S Herfried K. Wagner
> > M V P <URL:http://dotnet.mvps.org/>
> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
> >
> >
> >


Nov 21 '05 #12
Marina,
I still don't follow you. Do you plan on taking the first n characters, the
last n, the middle n, or a random sampling of n characters? Which sequence
(or collection) of n characters of a GUID do you think will even come close
to ensuring uniqueness? Without relying on an implementation detail of the
current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in other
words, as I stated, the GUID itself is what ensures uniqueness, not a subset
of it).

Also remember that a GUID is a structure that represents a 128-bit integer,
not a set of characters, the set of characters is just the human readable
representation of that structure.

NOTE: Reading
http://msdn.microsoft.com/library/de...classtopic.asp
even the full GUID has "a very low probability of being duplicated", which
means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Since the whole GUID is completely random and unique, a subset of it
should
be pretty random as well. Obviously, as you have less characters in a
string, the odds of getting repeats increases. However, if you still have
16
characters, the odds of this happening are still pretty low.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e3******** ******@TK2MSFTN GP09.phx.gbl...
Nina,
Yes. Considering the entire GUID is what is guaranteed on being unique.

GUID's are not sequential, they are seemingly random numbers, based on a
complicated formula that ensures uniqueness, so I'm not sure where or how
Marina is thinking you can truncate it.

Although GUID's are numbers I do not consider them numbers in the same

sense
as Integer, Double, or Decimal, as they are actually an identifier, a
Globally Unique IDentifier. Some articles use the term universally unique
identifier.

Hope this helps
Jay

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:03******** *************** ***********@mic rosoft.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> news:D2******** *************** ***********@mic rosoft.com...
>> > Thanks again. The Id that generated by GUID is very long. Is there
>> > any
>> way
>> > that I can generate a unique id with a specific length?
>> >
>> > "Herfried K. Wagner [MVP]" wrote:
>> >
>> > > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
>> > > > Thank you all for ideas and suggestions. Please tell me how
>> > > > does
>> > > > GUID
>> > > > work?
>> > > > In case, user exits the application, and lunches the application
>> > > > again
>> > > > later,
>> > > > could GUID generate same id that was generated in previous session? >> If
>> > > > this
>> > > > could happen then I cannot use it.
>> > >
>> > > That's very, very, very, ..., very unlikely. In practice it's
>> "guaranteed "
>> > > that every GUID only occurs once. There are various parameters used >> > > to
>> > > calculate the GUID. So you can use a GUID as ID without any fear
>> > > that
>> > > you'll get a GUID twice.
>> > >
>> > > --
>> > > M S Herfried K. Wagner
>> > > M V P <URL:http://dotnet.mvps.org/>
>> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>> > >
>> > >
>> > >
>>
>>
>>



Nov 21 '05 #13
Dim dt As DateTime = DateTime.Now
str = dt.ToString("Md yyHmsfff")

Guaranteed (provided the clock works) to be unique for 100 years or your
money back!
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uA******** ******@TK2MSFTN GP09.phx.gbl...
Marina,
I still don't follow you. Do you plan on taking the first n characters,
the last n, the middle n, or a random sampling of n characters? Which
sequence (or collection) of n characters of a GUID do you think will even
come close to ensuring uniqueness? Without relying on an implementation
detail of the current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in
other words, as I stated, the GUID itself is what ensures uniqueness, not
a subset of it).

Also remember that a GUID is a structure that represents a 128-bit
integer, not a set of characters, the set of characters is just the human
readable representation of that structure.

NOTE: Reading
http://msdn.microsoft.com/library/de...classtopic.asp
even the full GUID has "a very low probability of being duplicated", which
means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Since the whole GUID is completely random and unique, a subset of it
should
be pretty random as well. Obviously, as you have less characters in a
string, the odds of getting repeats increases. However, if you still have
16
characters, the odds of this happening are still pretty low.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e3******** ******@TK2MSFTN GP09.phx.gbl...
Nina,
Yes. Considering the entire GUID is what is guaranteed on being unique.

GUID's are not sequential, they are seemingly random numbers, based on a
complicated formula that ensures uniqueness, so I'm not sure where or
how
Marina is thinking you can truncate it.

Although GUID's are numbers I do not consider them numbers in the same

sense
as Integer, Double, or Decimal, as they are actually an identifier, a
Globally Unique IDentifier. Some articles use the term universally
unique
identifier.

Hope this helps
Jay

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:03******** *************** ***********@mic rosoft.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> news:D2******** *************** ***********@mic rosoft.com...
>> > Thanks again. The Id that generated by GUID is very long. Is
>> > there
>> > any
>> way
>> > that I can generate a unique id with a specific length?
>> >
>> > "Herfried K. Wagner [MVP]" wrote:
>> >
>> > > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
>> > > > Thank you all for ideas and suggestions. Please tell me how
>> > > > does
>> > > > GUID
>> > > > work?
>> > > > In case, user exits the application, and lunches the
>> > > > application
>> > > > again
>> > > > later,
>> > > > could GUID generate same id that was generated in previous

session?
>> If
>> > > > this
>> > > > could happen then I cannot use it.
>> > >
>> > > That's very, very, very, ..., very unlikely. In practice it's
>> "guaranteed "
>> > > that every GUID only occurs once. There are various parameters

used
>> > > to
>> > > calculate the GUID. So you can use a GUID as ID without any fear
>> > > that
>> > > you'll get a GUID twice.
>> > >
>> > > --
>> > > M S Herfried K. Wagner
>> > > M V P <URL:http://dotnet.mvps.org/>
>> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>> > >
>> > >
>> > >
>>
>>
>>



Nov 21 '05 #14
I would say that taking the first 16 characters, for example, will more or
less do it. It's not like GUIDs are based on something where it is just the
last characters that keep changing or something.

In theory, it may be that taking the first 16 characters would not guarantee
anything due to the GUID implementation. Yes, there might be some minute
possibility of a duplicate. But at some point you have to be practical.

And in practice, I think you will find that doing something like taking the
first 16 or 20 characters, will suffice.

And if the minute possibility that you might encounter a duplicate is still
troublesome because the system is so huge and critical, then the key column
for the table needs to be increased to support a GUID.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uA******** ******@TK2MSFTN GP09.phx.gbl...
Marina,
I still don't follow you. Do you plan on taking the first n characters, the last n, the middle n, or a random sampling of n characters? Which sequence
(or collection) of n characters of a GUID do you think will even come close to ensuring uniqueness? Without relying on an implementation detail of the
current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in other words, as I stated, the GUID itself is what ensures uniqueness, not a subset of it).

Also remember that a GUID is a structure that represents a 128-bit integer, not a set of characters, the set of characters is just the human readable
representation of that structure.

NOTE: Reading
http://msdn.microsoft.com/library/de...classtopic.asp even the full GUID has "a very low probability of being duplicated", which
means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Since the whole GUID is completely random and unique, a subset of it
should
be pretty random as well. Obviously, as you have less characters in a
string, the odds of getting repeats increases. However, if you still have 16
characters, the odds of this happening are still pretty low.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:e3******** ******@TK2MSFTN GP09.phx.gbl...
Nina,
Yes. Considering the entire GUID is what is guaranteed on being unique.

GUID's are not sequential, they are seemingly random numbers, based on a complicated formula that ensures uniqueness, so I'm not sure where or how Marina is thinking you can truncate it.

Although GUID's are numbers I do not consider them numbers in the same

sense
as Integer, Double, or Decimal, as they are actually an identifier, a
Globally Unique IDentifier. Some articles use the term universally unique identifier.

Hope this helps
Jay

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:03******** *************** ***********@mic rosoft.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> news:D2******** *************** ***********@mic rosoft.com...
>> > Thanks again. The Id that generated by GUID is very long. Is there >> > any
>> way
>> > that I can generate a unique id with a specific length?
>> >
>> > "Herfried K. Wagner [MVP]" wrote:
>> >
>> > > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
>> > > > Thank you all for ideas and suggestions. Please tell me how
>> > > > does
>> > > > GUID
>> > > > work?
>> > > > In case, user exits the application, and lunches the application >> > > > again
>> > > > later,
>> > > > could GUID generate same id that was generated in previous

session?
>> If
>> > > > this
>> > > > could happen then I cannot use it.
>> > >
>> > > That's very, very, very, ..., very unlikely. In practice it's
>> "guaranteed "
>> > > that every GUID only occurs once. There are various parameters

used
>> > > to
>> > > calculate the GUID. So you can use a GUID as ID without any fear >> > > that
>> > > you'll get a GUID twice.
>> > >
>> > > --
>> > > M S Herfried K. Wagner
>> > > M V P <URL:http://dotnet.mvps.org/>
>> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>> > >
>> > >
>> > >
>>
>>
>>



Nov 21 '05 #15
Marina,
Why mostly the first half, instead of mostly the second half or even mostly
the middle half? Isn't which half an implementation detail that might
change? Especially considering the change to CoCreateGUID from NT 4.0 to
Windows 2000, as suggested by:
http://support.microsoft.com/default...b;en-us;275280

Which GUID format are you suggesting using ("N", "D", "B", or "P")? As each
one causes different delimiters to be included.

For the different GUID formats & their lengths try:

Dim aGuid As Guid = Guid.NewGuid
Debug.WriteLine (aGuid.ToString ("N"), "N")
Debug.WriteLine (aGuid.ToString ("N").Length , "N")
Debug.WriteLine (aGuid.ToString ("D"), "D")
Debug.WriteLine (aGuid.ToString ("D").Length , "D")
Debug.WriteLine (aGuid.ToString ("B"), "B")
Debug.WriteLine (aGuid.ToString ("B").Length , "B")
Debug.WriteLine (aGuid.ToString ("P"), "P")
Debug.WriteLine (aGuid.ToString ("P").Length , "P")
Return

Remember a Guid is 128-bit (16 byte) value, so taking the first 16
characters of a GUID just doubled the memory you are using! (16 Chars = 32
Bytes)

Hence I find it safest to simply use the full 16-byte GUID, as again I would
not rely on IMHO a fragile design!

BTW: The following KB article is interesting:

http://support.microsoft.com/default...b;en-us;320375

I would considering using one of those the routines presented to create a
short GUID, instead of slicing & dicing System.Guid.

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:uW******** ******@tk2msftn gp13.phx.gbl...
I would say that taking the first 16 characters, for example, will more or
less do it. It's not like GUIDs are based on something where it is just
the
last characters that keep changing or something.

In theory, it may be that taking the first 16 characters would not
guarantee
anything due to the GUID implementation. Yes, there might be some minute
possibility of a duplicate. But at some point you have to be practical.

And in practice, I think you will find that doing something like taking
the
first 16 or 20 characters, will suffice.

And if the minute possibility that you might encounter a duplicate is
still
troublesome because the system is so huge and critical, then the key
column
for the table needs to be increased to support a GUID.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uA******** ******@TK2MSFTN GP09.phx.gbl...
Marina,
I still don't follow you. Do you plan on taking the first n characters,

the
last n, the middle n, or a random sampling of n characters? Which
sequence
(or collection) of n characters of a GUID do you think will even come

close
to ensuring uniqueness? Without relying on an implementation detail of
the
current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in

other
words, as I stated, the GUID itself is what ensures uniqueness, not a

subset
of it).

Also remember that a GUID is a structure that represents a 128-bit

integer,
not a set of characters, the set of characters is just the human readable
representation of that structure.

NOTE: Reading

http://msdn.microsoft.com/library/de...classtopic.asp
even the full GUID has "a very low probability of being duplicated",
which
means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
> Since the whole GUID is completely random and unique, a subset of it
> should
> be pretty random as well. Obviously, as you have less characters in a
> string, the odds of getting repeats increases. However, if you still have > 16
> characters, the odds of this happening are still pretty low.
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message > news:e3******** ******@TK2MSFTN GP09.phx.gbl...
>> Nina,
>> Yes. Considering the entire GUID is what is guaranteed on being
>> unique.
>>
>> GUID's are not sequential, they are seemingly random numbers, based on a >> complicated formula that ensures uniqueness, so I'm not sure where or how >> Marina is thinking you can truncate it.
>>
>> Although GUID's are numbers I do not consider them numbers in the same
> sense
>> as Integer, Double, or Decimal, as they are actually an identifier, a
>> Globally Unique IDentifier. Some articles use the term universally unique >> identifier.
>>
>> Hope this helps
>> Jay
>>
>> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> news:03******** *************** ***********@mic rosoft.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> >> news:D2******** *************** ***********@mic rosoft.com...
>> >> > Thanks again. The Id that generated by GUID is very long. Is there >> >> > any
>> >> way
>> >> > that I can generate a unique id with a specific length?
>> >> >
>> >> > "Herfried K. Wagner [MVP]" wrote:
>> >> >
>> >> > > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
>> >> > > > Thank you all for ideas and suggestions. Please tell me how
>> >> > > > does
>> >> > > > GUID
>> >> > > > work?
>> >> > > > In case, user exits the application, and lunches the application >> >> > > > again
>> >> > > > later,
>> >> > > > could GUID generate same id that was generated in previous
> session?
>> >> If
>> >> > > > this
>> >> > > > could happen then I cannot use it.
>> >> > >
>> >> > > That's very, very, very, ..., very unlikely. In practice it's
>> >> "guaranteed "
>> >> > > that every GUID only occurs once. There are various parameters
> used
>> >> > > to
>> >> > > calculate the GUID. So you can use a GUID as ID without any fear >> >> > > that
>> >> > > you'll get a GUID twice.
>> >> > >
>> >> > > --
>> >> > > M S Herfried K. Wagner
>> >> > > M V P <URL:http://dotnet.mvps.org/>
>> >> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>> >> > >
>> >> > >
>> >> > >
>> >>
>> >>
>> >>
>>
>>
>
>



Nov 21 '05 #16
Nina,
In addition to our lively debate on truncating System.Guid, the following KB
article has a handful of routines that create "guids" based on the system
time.

http://support.microsoft.com/default...b;en-us;320375

Hope this helps
Jay

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:36******** *************** ***********@mic rosoft.com...
I need to generate unique id for my application. Use system time to create
id, which is the best fit, but I don't know how to do it. Please help.

Thanks in advance for any input.

Nov 21 '05 #17
John,
KA BOOM!!!! Your sample just gave me hundreds of duplicate entries!

At the very least you may want to include ALL 7 digits for milliseconds!
Plus full width (both digits) for months, days, hours, minutes & seconds!
str = dt.ToString("MM ddyyHHmmssfffff ff")
As your string will give duplicates on for Jan 11th & Nov 1st, plus it will
give duplicates between for 11:01 & 1:11!

To see what I mean try the following:

Dim dt1 As DateTime = #1/11/2005 11:01:00 AM#
Dim dt2 As DateTime = #11/1/2005 1:11:00 AM#
Dim str1 As String = dt1.ToString("M dyyHmsfff")
Dim str2 As String = dt2.ToString("M dyyHmsfff")

Debug.WriteLine (str1, dt1.ToString())
Debug.WriteLine (str2, dt2.ToString())

Debug.WriteLine (str1 = str2)
Of course when the clock falls back for daylight savings time, there will be
an hour where duplicates are also created! Also if you explicitly adjust
your clock or Windows adjusts your clock, you will have another opportunity
for duplicates.

My money is on using the full "precision" of System.Guid.

Hope this helps
Jay

"JohnBoy" <do******@nospa m.com> wrote in message
news:10******** *****@corp.supe rnews.com... Dim dt As DateTime = DateTime.Now
str = dt.ToString("Md yyHmsfff")

Guaranteed (provided the clock works) to be unique for 100 years or your
money back!
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uA******** ******@TK2MSFTN GP09.phx.gbl...
Marina,
I still don't follow you. Do you plan on taking the first n characters,
the last n, the middle n, or a random sampling of n characters? Which
sequence (or collection) of n characters of a GUID do you think will even
come close to ensuring uniqueness? Without relying on an implementation
detail of the current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in
other words, as I stated, the GUID itself is what ensures uniqueness, not
a subset of it).

Also remember that a GUID is a structure that represents a 128-bit
integer, not a set of characters, the set of characters is just the human
readable representation of that structure.

NOTE: Reading
http://msdn.microsoft.com/library/de...classtopic.asp
even the full GUID has "a very low probability of being duplicated",
which means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Since the whole GUID is completely random and unique, a subset of it
should
be pretty random as well. Obviously, as you have less characters in a
string, the odds of getting repeats increases. However, if you still
have 16
characters, the odds of this happening are still pretty low.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in
message
news:e3******** ******@TK2MSFTN GP09.phx.gbl...
Nina,
Yes. Considering the entire GUID is what is guaranteed on being unique.

GUID's are not sequential, they are seemingly random numbers, based on
a
complicated formula that ensures uniqueness, so I'm not sure where or
how
Marina is thinking you can truncate it.

Although GUID's are numbers I do not consider them numbers in the same
sense
as Integer, Double, or Decimal, as they are actually an identifier, a
Globally Unique IDentifier. Some articles use the term universally
unique
identifier.

Hope this helps
Jay

"Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
news:03******** *************** ***********@mic rosoft.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> news:D2******** *************** ***********@mic rosoft.com...
>> > Thanks again. The Id that generated by GUID is very long. Is
>> > there
>> > any
>> way
>> > that I can generate a unique id with a specific length?
>> >
>> > "Herfried K. Wagner [MVP]" wrote:
>> >
>> > > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
>> > > > Thank you all for ideas and suggestions. Please tell me how
>> > > > does
>> > > > GUID
>> > > > work?
>> > > > In case, user exits the application, and lunches the
>> > > > application
>> > > > again
>> > > > later,
>> > > > could GUID generate same id that was generated in previous
session?
>> If
>> > > > this
>> > > > could happen then I cannot use it.
>> > >
>> > > That's very, very, very, ..., very unlikely. In practice it's
>> "guaranteed "
>> > > that every GUID only occurs once. There are various parameters
used
>> > > to
>> > > calculate the GUID. So you can use a GUID as ID without any
>> > > fear
>> > > that
>> > > you'll get a GUID twice.
>> > >
>> > > --
>> > > M S Herfried K. Wagner
>> > > M V P <URL:http://dotnet.mvps.org/>
>> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>> > >
>> > >
>> > >
>>
>>
>>



Nov 21 '05 #18
I should add that if you need a string, and you want to ensure its as short
as possible I would look at encoding the (entire) Guid.ToByteArra y value in
a format other then hex, possible base64 or some custom scheme that used
upper & lower case "ASCII" letters & numbers...

Similar to the GuidTest3.asp & GuidTest4.asp samples at
http://support.microsoft.com/default...b;en-us;320375

Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:Oe******** ******@TK2MSFTN GP15.phx.gbl...
Marina,
Why mostly the first half, instead of mostly the second half or even
mostly the middle half? Isn't which half an implementation detail that
might change? Especially considering the change to CoCreateGUID from NT
4.0 to Windows 2000, as suggested by:
http://support.microsoft.com/default...b;en-us;275280

Which GUID format are you suggesting using ("N", "D", "B", or "P")? As
each one causes different delimiters to be included.

For the different GUID formats & their lengths try:

Dim aGuid As Guid = Guid.NewGuid
Debug.WriteLine (aGuid.ToString ("N"), "N")
Debug.WriteLine (aGuid.ToString ("N").Length , "N")
Debug.WriteLine (aGuid.ToString ("D"), "D")
Debug.WriteLine (aGuid.ToString ("D").Length , "D")
Debug.WriteLine (aGuid.ToString ("B"), "B")
Debug.WriteLine (aGuid.ToString ("B").Length , "B")
Debug.WriteLine (aGuid.ToString ("P"), "P")
Debug.WriteLine (aGuid.ToString ("P").Length , "P")
Return

Remember a Guid is 128-bit (16 byte) value, so taking the first 16
characters of a GUID just doubled the memory you are using! (16 Chars = 32
Bytes)

Hence I find it safest to simply use the full 16-byte GUID, as again I
would not rely on IMHO a fragile design!

BTW: The following KB article is interesting:

http://support.microsoft.com/default...b;en-us;320375

I would considering using one of those the routines presented to create a
short GUID, instead of slicing & dicing System.Guid.

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:uW******** ******@tk2msftn gp13.phx.gbl...
I would say that taking the first 16 characters, for example, will more or
less do it. It's not like GUIDs are based on something where it is just
the
last characters that keep changing or something.

In theory, it may be that taking the first 16 characters would not
guarantee
anything due to the GUID implementation. Yes, there might be some minute
possibility of a duplicate. But at some point you have to be practical.

And in practice, I think you will find that doing something like taking
the
first 16 or 20 characters, will suffice.

And if the minute possibility that you might encounter a duplicate is
still
troublesome because the system is so huge and critical, then the key
column
for the table needs to be increased to support a GUID.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uA******** ******@TK2MSFTN GP09.phx.gbl...
Marina,
I still don't follow you. Do you plan on taking the first n characters,

the
last n, the middle n, or a random sampling of n characters? Which
sequence
(or collection) of n characters of a GUID do you think will even come

close
to ensuring uniqueness? Without relying on an implementation detail of
the
current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in

other
words, as I stated, the GUID itself is what ensures uniqueness, not a

subset
of it).

Also remember that a GUID is a structure that represents a 128-bit

integer,
not a set of characters, the set of characters is just the human
readable
representation of that structure.

NOTE: Reading

http://msdn.microsoft.com/library/de...classtopic.asp
even the full GUID has "a very low probability of being duplicated",
which
means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
> Since the whole GUID is completely random and unique, a subset of it
> should
> be pretty random as well. Obviously, as you have less characters in a
> string, the odds of getting repeats increases. However, if you still

have
> 16
> characters, the odds of this happening are still pretty low.
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in

message
> news:e3******** ******@TK2MSFTN GP09.phx.gbl...
>> Nina,
>> Yes. Considering the entire GUID is what is guaranteed on being
>> unique.
>>
>> GUID's are not sequential, they are seemingly random numbers, based
>> on

a
>> complicated formula that ensures uniqueness, so I'm not sure where or

how
>> Marina is thinking you can truncate it.
>>
>> Although GUID's are numbers I do not consider them numbers in the
>> same
> sense
>> as Integer, Double, or Decimal, as they are actually an identifier, a
>> Globally Unique IDentifier. Some articles use the term universally

unique
>> identifier.
>>
>> Hope this helps
>> Jay
>>
>> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> news:03******** *************** ***********@mic rosoft.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> >> news:D2******** *************** ***********@mic rosoft.com...
>> >> > Thanks again. The Id that generated by GUID is very long. Is

there
>> >> > any
>> >> way
>> >> > that I can generate a unique id with a specific length?
>> >> >
>> >> > "Herfried K. Wagner [MVP]" wrote:
>> >> >
>> >> > > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
>> >> > > > Thank you all for ideas and suggestions. Please tell me how
>> >> > > > does
>> >> > > > GUID
>> >> > > > work?
>> >> > > > In case, user exits the application, and lunches the

application
>> >> > > > again
>> >> > > > later,
>> >> > > > could GUID generate same id that was generated in previous
> session?
>> >> If
>> >> > > > this
>> >> > > > could happen then I cannot use it.
>> >> > >
>> >> > > That's very, very, very, ..., very unlikely. In practice it's
>> >> "guaranteed "
>> >> > > that every GUID only occurs once. There are various
>> >> > > parameters
> used
>> >> > > to
>> >> > > calculate the GUID. So you can use a GUID as ID without any

fear
>> >> > > that
>> >> > > you'll get a GUID twice.
>> >> > >
>> >> > > --
>> >> > > M S Herfried K. Wagner
>> >> > > M V P <URL:http://dotnet.mvps.org/>
>> >> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>> >> > >
>> >> > >
>> >> > >
>> >>
>> >>
>> >>
>>
>>
>
>



Nov 21 '05 #19
I didn't say it has to be the first half. Take any part of it you like.
Geez. Are you just picking at everything and making it seem more
complicated then it is for a reason? Take some part of it that you like, get
rid of all delimiters, and in a practical case, you are going to get unique
strings.

Who cares about using slightly more memory or whatever? This wasn't supposed
to be the most memory efficient method of making sure you save every bit of
memory possible. It was a quick and easy way to generate a random string to
be used as a primary key. And like I said, in practical scenarios, this
works quite well.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:Oe******** ******@TK2MSFTN GP15.phx.gbl...
Marina,
Why mostly the first half, instead of mostly the second half or even mostly the middle half? Isn't which half an implementation detail that might
change? Especially considering the change to CoCreateGUID from NT 4.0 to
Windows 2000, as suggested by:
http://support.microsoft.com/default...b;en-us;275280

Which GUID format are you suggesting using ("N", "D", "B", or "P")? As each one causes different delimiters to be included.

For the different GUID formats & their lengths try:

Dim aGuid As Guid = Guid.NewGuid
Debug.WriteLine (aGuid.ToString ("N"), "N")
Debug.WriteLine (aGuid.ToString ("N").Length , "N")
Debug.WriteLine (aGuid.ToString ("D"), "D")
Debug.WriteLine (aGuid.ToString ("D").Length , "D")
Debug.WriteLine (aGuid.ToString ("B"), "B")
Debug.WriteLine (aGuid.ToString ("B").Length , "B")
Debug.WriteLine (aGuid.ToString ("P"), "P")
Debug.WriteLine (aGuid.ToString ("P").Length , "P")
Return

Remember a Guid is 128-bit (16 byte) value, so taking the first 16
characters of a GUID just doubled the memory you are using! (16 Chars = 32
Bytes)

Hence I find it safest to simply use the full 16-byte GUID, as again I would not rely on IMHO a fragile design!

BTW: The following KB article is interesting:

http://support.microsoft.com/default...b;en-us;320375

I would considering using one of those the routines presented to create a
short GUID, instead of slicing & dicing System.Guid.

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:uW******** ******@tk2msftn gp13.phx.gbl...
I would say that taking the first 16 characters, for example, will more or
less do it. It's not like GUIDs are based on something where it is just
the
last characters that keep changing or something.

In theory, it may be that taking the first 16 characters would not
guarantee
anything due to the GUID implementation. Yes, there might be some minute
possibility of a duplicate. But at some point you have to be practical.

And in practice, I think you will find that doing something like taking
the
first 16 or 20 characters, will suffice.

And if the minute possibility that you might encounter a duplicate is
still
troublesome because the system is so huge and critical, then the key
column
for the table needs to be increased to support a GUID.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:uA******** ******@TK2MSFTN GP09.phx.gbl...
Marina,
I still don't follow you. Do you plan on taking the first n characters,

the
last n, the middle n, or a random sampling of n characters? Which
sequence
(or collection) of n characters of a GUID do you think will even come

close
to ensuring uniqueness? Without relying on an implementation detail of
the
current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in

other
words, as I stated, the GUID itself is what ensures uniqueness, not a

subset
of it).

Also remember that a GUID is a structure that represents a 128-bit

integer,
not a set of characters, the set of characters is just the human readable representation of that structure.

NOTE: Reading

http://msdn.microsoft.com/library/de...classtopic.asp
even the full GUID has "a very low probability of being duplicated",
which
means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay

"Marina" <so*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
> Since the whole GUID is completely random and unique, a subset of it
> should
> be pretty random as well. Obviously, as you have less characters in a
> string, the odds of getting repeats increases. However, if you still

have
> 16
> characters, the odds of this happening are still pretty low.
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in

message
> news:e3******** ******@TK2MSFTN GP09.phx.gbl...
>> Nina,
>> Yes. Considering the entire GUID is what is guaranteed on being
>> unique.
>>
>> GUID's are not sequential, they are seemingly random numbers, based on a
>> complicated formula that ensures uniqueness, so I'm not sure where
or how
>> Marina is thinking you can truncate it.
>>
>> Although GUID's are numbers I do not consider them numbers in the

same > sense
>> as Integer, Double, or Decimal, as they are actually an identifier, a >> Globally Unique IDentifier. Some articles use the term universally

unique
>> identifier.
>>
>> Hope this helps
>> Jay
>>
>> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> news:03******** *************** ***********@mic rosoft.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussio ns.microsoft.co m> wrote in message
>> >> news:D2******** *************** ***********@mic rosoft.com...
>> >> > Thanks again. The Id that generated by GUID is very long. Is

there
>> >> > any
>> >> way
>> >> > that I can generate a unique id with a specific length?
>> >> >
>> >> > "Herfried K. Wagner [MVP]" wrote:
>> >> >
>> >> > > "Nina" <Ni**@discussio ns.microsoft.co m> schrieb:
>> >> > > > Thank you all for ideas and suggestions. Please tell me how >> >> > > > does
>> >> > > > GUID
>> >> > > > work?
>> >> > > > In case, user exits the application, and lunches the

application
>> >> > > > again
>> >> > > > later,
>> >> > > > could GUID generate same id that was generated in previous
> session?
>> >> If
>> >> > > > this
>> >> > > > could happen then I cannot use it.
>> >> > >
>> >> > > That's very, very, very, ..., very unlikely. In practice it's >> >> "guaranteed "
>> >> > > that every GUID only occurs once. There are various parameters > used
>> >> > > to
>> >> > > calculate the GUID. So you can use a GUID as ID without any

fear
>> >> > > that
>> >> > > you'll get a GUID twice.
>> >> > >
>> >> > > --
>> >> > > M S Herfried K. Wagner
>> >> > > M V P <URL:http://dotnet.mvps.org/>
>> >> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>> >> > >
>> >> > >
>> >> > >
>> >>
>> >>
>> >>
>>
>>
>
>



Nov 21 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3234
by: Mullin | last post by:
I need to generate a unique no. in the format like yyyymmddxxxxxx (xxxxxx 6-digitl running number) I think of create a table with two columns date value 20050405 120 20050406 99 Everytime, the application will increase the value+1 based on the same
10
18625
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from tablename; Here this function generate unique id for each row of the table. Regards..
1
11270
by: hikums | last post by:
I am posting this here, just in case anyone may need this. Step 1: CREATE SEQUENCE ID_SEQ START WITH 1050000 INCREMENT BY 1 MAXVALUE 9999999 NO CYCLE NO CACHE ORDER
11
16230
by: Kovan Akrei | last post by:
Hi, I wonder if there are any others ways (mehtods, properties) to get the system time than DateTime.Now.Ticks Regards from Kovan __________________________________________________________________ R u kidding me Ya right ICQ#: 149146797 Current ICQ status: + More ways to contact me __________________________________________________________________
3
1589
by: palani12kumar | last post by:
i want to generate a random number by using the system time. i dont know how to do it. And another thing is, how to find the millisecond? please help me
15
37600
by: Ashish Khandelwal | last post by:
As MSDN is not giving us guarantee upon uniqueness of Hash Code, so could any one suggest me that how to generate a unique Hash Code for same string always, and generate different-2 Hash Code Different-2 string.
4
2848
by: gaurav1983 | last post by:
i have to generate unique combinations of given number of digits entered by user eg: N=4 (0,1,2,3) output should be 0 1 2 3
1
2751
by: situ | last post by:
Hi, I'm using DB2 V9 for windows I'm inserting records into DGTT from select statement; the problem is how to insert unique value for every row inserted. I tried using “generated always as” clause but its exhausting at some point of time. Say if 100000 records were needed to be processed, it’s only processing up to 60000 rows.
6
2037
by: er | last post by:
hi, here's why i'm trying to do: header1.hpp namespace{ struct A{};} struct B1{ A a; }; header2.hpp namespace{ struct A{};}
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10395
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.