Does this code generate unique integers?

  • Thread starter Thread starter Mark S.
  • Start date Start date
M

Mark S.

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

No, it does not. You need to use the Random class.
 
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.
 
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).
 
// this will run for a while...
int iUnique;
for (iUnique = int.MinValue; iUnique < int.MaxValue; ++iUnique)
{
// do something unique...
}
 
Mark said:
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
 
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
 
Hoss said:
Just interpret the bytes that represent the guid as an integer

C# does not have 128 bit integers ...

Arne
 
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
 
Mark said:
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....


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 said:
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
 
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.
 
That was what I suggested, but I guess i was too subtle. Apparenly everyone
else seems to think unique implies non-sequential...
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top