Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Does this code generate unique integers?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark S. - 06 Jul 2007 17:59 GMT
I'm a fan of the GUID, but the current project is looking to use a genuinely
unique integer. Does the following do that?

Math.Abs(System.Guid.NewGuid().GetHashCode())

TIA
Rene - 06 Jul 2007 18:38 GMT
Guid == 128 bits
Guid.GetHashcode() == Int == 32 bits

What this means is that eventually, you will get repeated int values.

What are the chances of getting a repeated value? I guess the answer to that
question will depend on the algorithm used by the Guid object to generate
the hash code.

If it's critical to create unique values then this solution may not work, if
you do some error trapping in case the int gets repeated and fix the problem
then, my guess is that you would be ok because I would thing that the
chances of getting a repeated value are slim.

> I'm a fan of the GUID, but the current project is looking to use a
> genuinely unique integer. Does the following do that?
>
> Math.Abs(System.Guid.NewGuid().GetHashCode())
>
> TIA
DanielGifford@gmail.com - 06 Jul 2007 18:41 GMT
> I'm a fan of the GUID, but the current project is looking to use a genuinely
> unique integer. Does the following do that?
>
> Math.Abs(System.Guid.NewGuid().GetHashCode())
>
> TIA

No, it does not.  You need to use the Random class.
Nicholas Paldino [.NET/C# MVP] - 06 Jul 2007 19:37 GMT
This is not guaranteed to work either.  Random makes no promises as to
how unique the number is, it is just random (and predictable at that).  If
you really want real random numbers, then you should be using a
cryptographic random number generator, which tends to be much more random.

   However, the original problem of not duplicating a number in the
sequence remains.

   To that end, the OP should use a GUID.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>> I'm a fan of the GUID, but the current project is looking to use a
>> genuinely
[quoted text clipped - 5 lines]
>
> No, it does not.  You need to use the Random class.
Nicholas Paldino [.NET/C# MVP] - 06 Jul 2007 19:41 GMT
Mark,

   No, it does not.  Must the integer be 32 bits, or can you use a 128 bit
number (in which case, you use the GUID).

   If you are limited to 32 bits, then you are probably better off just
maintaining an id somewhere which you increment every time you need a new
one.  This way, you don't have to worry about conflicts until you generate
2^32 of them (assuming you are using an unsigned integer).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> I'm a fan of the GUID, but the current project is looking to use a
> genuinely unique integer. Does the following do that?
>
> Math.Abs(System.Guid.NewGuid().GetHashCode())
>
> TIA
Jeroen Langen, van - 28 Sep 2007 15:23 GMT
I agree to Nicholas Paldino. U can't generate a unique number with hash
or random numbers.

Here's an example u can use:

public class UniqueInt
{
 private static int _uniqueInt = 0;

 public static int Get()
 {
   Interlocked.Increment(ref _uniqueInt);
   return _uniqueInt;
 }

}

Each time u use the static Get() method it will gave a unique number
until it wraps. This number is only unique per session.
ModelBuilder - 06 Jul 2007 19:44 GMT
// this will run for a while...
int iUnique;
for (iUnique = int.MinValue; iUnique < int.MaxValue; ++iUnique)
{
 // do something unique...
}

> I'm a fan of the GUID, but the current project is looking to use a genuinely
> unique integer. Does the following do that?
>
> Math.Abs(System.Guid.NewGuid().GetHashCode())
>
> TIA
Arne Vajhøj - 07 Jul 2007 00:45 GMT
> I'm a fan of the GUID, but the current project is looking to use a genuinely
> unique integer. Does the following do that?
>
> Math.Abs(System.Guid.NewGuid().GetHashCode())

No.

For non persistent single system context use a singleton with a counter.

For persistent or multi system context use Scott Ambler high low
pattern and a database.

Arne
Hoss - 07 Jul 2007 01:13 GMT
> I'm a fan of the GUID, but the current project is looking to use a genuinely
> unique integer. Does the following do that?
>
> Math.Abs(System.Guid.NewGuid().GetHashCode())
>
> TIA

Just interpret the bytes that represent the guid as an integer
Arne Vajhøj - 07 Jul 2007 02:00 GMT
>> I'm a fan of the GUID, but the current project is looking to use a genuinely
>> unique integer. Does the following do that?
> Just interpret the bytes that represent the guid as an integer

C# does not have 128 bit integers ...

Arne
Mark S. - 07 Jul 2007 02:20 GMT
Thank you all. The incrementing ideas makes sense under the conditions
described. However, I was just informed I can go as large as a BigInt (in
SQL Server 2005). In this case I'm drawn to Hoss's idea....

> Just interpret the bytes that represent the guid as an integer

Hoss, any thoughts on the most optimized way to get the guid bytes into a
represented bigint?

Thank you.

SQL Server 2005 definitions:
bigint
-2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
8 Bytes

int
-2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)
4 Bytes
Arne Vajhøj - 07 Jul 2007 02:28 GMT
> Thank you all. The incrementing ideas makes sense under the conditions
> described. However, I was just informed I can go as large as a BigInt (in
[quoted text clipped - 4 lines]
> Hoss, any thoughts on the most optimized way to get the guid bytes into a
> represented bigint?

Still hard to squeeze 128 bit down into 64 bit.

Arne
Mark S. - 07 Jul 2007 03:01 GMT
good point. i'll see if the db guys can give up a bigger data type.
Arne Vajhøj - 07 Jul 2007 03:47 GMT
> good point. i'll see if the db guys can give up a bigger data type.

SQLServer has a GUID data type called uniqueidentifier.

Arne
AlexS - 08 Jul 2007 03:40 GMT
Every integer is unique.

So, you can start from 1, then 2 and so on up to your max value.

Just keep somewhere last used id and get new one by adding 1 to it. With
boundary checking of course.

> I'm a fan of the GUID, but the current project is looking to use a
> genuinely unique integer. Does the following do that?
>
> Math.Abs(System.Guid.NewGuid().GetHashCode())
>
> TIA
ModelBuilder - 09 Jul 2007 03:56 GMT
That was what I suggested, but I guess i was too subtle.  Apparenly everyone
else seems to think unique implies non-sequential...

> Every integer is unique.
>
[quoted text clipped - 9 lines]
> >
> > TIA
halplus shadowbit - 14 Jul 2007 12:41 GMT
try this one:
http://msdn2.microsoft.com/en-us/library/system.guid.aspx

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.