By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,633 Members | 1,931 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,633 IT Pros & Developers. It's quick & easy.

How to use system time to generate unique id?

P: n/a
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 #1
Share this Question
Share on Google+
28 Replies


P: n/a
What about using the Guid class to generate a new Guid?

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.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 #2

P: n/a
GUID is a good way to get a unique id but if you want your id to have some
relationship to the time why not try using the number of days, minutes, or
seconds since the file was created until it was last updated? Actually, I
beleive the applications productBuildPart information gives you the number
of days since January 1, 2000 to the last day the file was modified?

System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Lo cation).ProductBuildPart

If you want the time, in this case using minutes, between when the file was
created to when the file was last modified try -

System.IO.File.GetLastWriteTime( _
System.Reflection.Assembly.GetExecutingAssembly.Lo cation).Subtract( _
System.IO.File.GetCreationTime( _
System.Reflection.Assembly.GetExecutingAssembly.Lo cation)).TotalMinutes

Dave

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.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 #3

P: n/a
"Nina" <Ni**@discussions.microsoft.com> schrieb:
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.


The simplest solution is using a GUID:

\\\
Dim ID As String = Guid.NewGuid().ToString()
///

--
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 #4

P: n/a
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.

"Herfried K. Wagner [MVP]" wrote:
"Nina" <Ni**@discussions.microsoft.com> schrieb:
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.


The simplest solution is using a GUID:

\\\
Dim ID As String = Guid.NewGuid().ToString()
///

--
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 #5

P: n/a
"Nina" <Ni**@discussions.microsoft.com> 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 #6

P: n/a
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**@discussions.microsoft.com> 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 #7

P: n/a
There is nothing stopping you from truncating it.

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #8

P: n/a
Thanks. Will truncate causing duplication?

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

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #9

P: n/a
If you truncated it, wouldn't that introduce the possiblity (albeit slight)
of non-uniqueness?

What if you didn't pass it around as a string, but kept it in its native
datatype? It is only 16 bytes in SQL server when stored as uniqueidentifier
datatype. Versus the ~36 bytes for the equivalent string.

Greg

"Marina" <so*****@nospam.com> wrote in message
news:Oa**************@tk2msftngp13.phx.gbl...
There is nothing stopping you from truncating it.

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #10

P: n/a
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**@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
Thanks. Will truncate causing duplication?

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

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
Thanks. Will truncate causing duplication?

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

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
Dim dt As DateTime = DateTime.Now
str = dt.ToString("MdyyHmsfff")

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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
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**************@tk2msftngp13.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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>> news:03**********************************@microsof t.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
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**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.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

P: n/a
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("MMddyyHHmmssfffffff")
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("MdyyHmsfff")
Dim str2 As String = dt2.ToString("MdyyHmsfff")

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******@nospam.com> wrote in message
news:10*************@corp.supernews.com... Dim dt As DateTime = DateTime.Now
str = dt.ToString("MdyyHmsfff")

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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
> Thanks. Will truncate causing duplication?
>
> "Marina" wrote:
>
>> There is nothing stopping you from truncating it.
>>
>> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
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.ToByteArray 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**************@TK2MSFTNGP15.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**************@tk2msftngp13.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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>> news:03**********************************@microsof t.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
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**************@TK2MSFTNGP15.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**************@tk2msftngp13.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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>> news:03**********************************@microsof t.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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

P: n/a
I would additionally like to point out, that if using letters and numbers,
we are talking 36^16 different possibilities for a GUID, if using the first
half of a GUID (without delimiters). I don't know about you, but that is
enough for me.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oe**************@TK2MSFTNGP15.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**************@tk2msftngp13.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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>> news:03**********************************@microsof t.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #21

P: n/a
Marina,
I'm sorry, I'm concerned you are missing my point.

1) that a System.Guid is more assured of being unique then a subset of it.
2) that taking a subset of it doesn't appear to buy you anything (in terms
of uniqueness or memory).

Ergo using a System.Guid itself appears to be better then using a subset!

As I stated: I will stay with Guid itself, rather then a fragile design. If
you or others favor fragile designs, hopefully I do not inherit them.

Enough of this, I can tell I am really not going to convince you, and you
are not going to convince me. Thanks for the interesting discussion...

Later
Jay

"Marina" <so*****@nospam.com> wrote in message
news:uK**************@TK2MSFTNGP15.phx.gbl...
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**************@TK2MSFTNGP15.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**************@tk2msftngp13.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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>> >> news:03**********************************@microsof t.com...
>> >> > Thanks. Will truncate causing duplication?
>> >> >
>> >> > "Marina" wrote:
>> >> >
>> >> >> There is nothing stopping you from truncating it.
>> >> >>
>> >> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> >> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #22

P: n/a
Marina,

Jay is from the US (maybe as you), he thinks probably that larger numbers
gives more security.

When I buy a lottery ticket here in this small country, I have probably as
much change to win as there in the US.

(In fact Jay is right of course, the larger the number the less change there
is on duplicates, however complete sure you are never how long the number
will be)

I hope that I clear with this, that we agree as almost forever again, (and
not disagree with Jay before he thinks that)?

:-)

Cor
Nov 21 '05 #23

P: n/a
Geez, chill out!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl...
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("MMddyyHHmmssfffffff")


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("MdyyHmsfff")
Dim str2 As String = dt2.ToString("MdyyHmsfff")

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******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Dim dt As DateTime = DateTime.Now
str = dt.ToString("MdyyHmsfff")

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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
> news:03**********************************@microsof t.com...
> > Thanks. Will truncate causing duplication?
> >
> > "Marina" wrote:
> >
> >> There is nothing stopping you from truncating it.
> >>
> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #24

P: n/a
John,
Odd, I thought I was "chill" and had attempted to explain rather nicely &
politely why your routine would very quickly give duplicates.

Just a thought
Jay

"JohnBoy" <do******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Geez, chill out!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl...
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("MMddyyHHmmssfffffff")


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("MdyyHmsfff")
Dim str2 As String = dt2.ToString("MdyyHmsfff")

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******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Dim dt As DateTime = DateTime.Now
str = dt.ToString("MdyyHmsfff")

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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>> news:03**********************************@microsof t.com...
>> > Thanks. Will truncate causing duplication?
>> >
>> > "Marina" wrote:
>> >
>> >> There is nothing stopping you from truncating it.
>> >>
>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #25

P: n/a
Anal Jay,

Keeps your "thoughts" to yourself.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...
John,
Odd, I thought I was "chill" and had attempted to explain rather nicely &
politely why your routine would very quickly give duplicates.

Just a thought
Jay

"JohnBoy" <do******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Geez, chill out!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl...
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("MMddyyHHmmssfffffff")

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("MdyyHmsfff")
Dim str2 As String = dt2.ToString("MdyyHmsfff")

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******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Dim dt As DateTime = DateTime.Now
str = dt.ToString("MdyyHmsfff")

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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>>> news:03**********************************@microsof t.com...
>>> > Thanks. Will truncate causing duplication?
>>> >
>>> > "Marina" wrote:
>>> >
>>> >> There is nothing stopping you from truncating it.
>>> >>
>>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #26

P: n/a
Thank you so much for your help.

"Jay B. Harlow [MVP - Outlook]" wrote:
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**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.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 #27

P: n/a
JohnBoy,
Such professionalism!

Seeing as I live in a free country that prides itself on freedom of speech.
I will not even presume to suggest that any one should keep their thoughts
to themselves! In other words you are totally within your rights to think of
me as you will, and to state that opinion here. Others reading your
assessment are equally free to agree or disagree with your opinion.

Me personally, your personal comments about me are immaterial! I will
continue providing the highest level of support that I am able to!

Have a nice day.
Jay

"JohnBoy" <do******@nospam.com> wrote in message
news:10************@corp.supernews.com...
Anal Jay,

Keeps your "thoughts" to yourself.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...
John,
Odd, I thought I was "chill" and had attempted to explain rather nicely &
politely why your routine would very quickly give duplicates.

Just a thought
Jay

"JohnBoy" <do******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Geez, chill out!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:OR**************@TK2MSFTNGP11.phx.gbl...
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("MMddyyHHmmssfffffff")

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("MdyyHmsfff")
Dim str2 As String = dt2.ToString("MdyyHmsfff")

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******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
> Dim dt As DateTime = DateTime.Now
> str = dt.ToString("MdyyHmsfff")
>
> 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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>>>> news:03**********************************@microsof t.com...
>>>> > Thanks. Will truncate causing duplication?
>>>> >
>>>> > "Marina" wrote:
>>>> >
>>>> >> There is nothing stopping you from truncating it.
>>>> >>
>>>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>>>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #28

P: n/a
Jay,

In my opinion it was a very correct answer you did, when I had seen it
directly I had probebly done it as well.

You know my standard answer on this, when people start throwing mud in a
discussion, they lost it and show only something about themselves.

Cor

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com>
JohnBoy,
Such professionalism!

Seeing as I live in a free country that prides itself on freedom of
speech. I will not even presume to suggest that any one should keep their
thoughts to themselves! In other words you are totally within your rights
to think of me as you will, and to state that opinion here. Others reading
your assessment are equally free to agree or disagree with your opinion.

Me personally, your personal comments about me are immaterial! I will
continue providing the highest level of support that I am able to!

Have a nice day.
Jay

"JohnBoy" <do******@nospam.com> wrote in message
news:10************@corp.supernews.com...
Anal Jay,

Keeps your "thoughts" to yourself.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...
John,
Odd, I thought I was "chill" and had attempted to explain rather nicely
& politely why your routine would very quickly give duplicates.

Just a thought
Jay

"JohnBoy" <do******@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Geez, chill out!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:OR**************@TK2MSFTNGP11.phx.gbl...
> 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("MMddyyHHmmssfffffff")
>
> 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("MdyyHmsfff")
> Dim str2 As String = dt2.ToString("MdyyHmsfff")
>
> 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******@nospam.com> wrote in message
> news:10*************@corp.supernews.com...
>> Dim dt As DateTime = DateTime.Now
>> str = dt.ToString("MdyyHmsfff")
>>
>> 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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.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**@discussions.microsoft.com> wrote in message
>>>>> news:03**********************************@microsof t.com...
>>>>> > Thanks. Will truncate causing duplication?
>>>>> >
>>>>> > "Marina" wrote:
>>>>> >
>>>>> >> There is nothing stopping you from truncating it.
>>>>> >>
>>>>> >> "Nina" <Ni**@discussions.microsoft.com> wrote in message
>>>>> >> news:D2**********************************@microsof t.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**@discussions.microsoft.com> 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 #29

This discussion thread is closed

Replies have been disabled for this discussion.