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

Does this code generate unique integers?

P: n/a
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
Jul 6 '07 #1
Share this Question
Share on Google+
16 Replies


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


"Mark S." <ma***@yahoo.comwrote in message
news:eL****************@TK2MSFTNGP02.phx.gbl...
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

Jul 6 '07 #2

P: n/a
On Jul 6, 12:59 pm, "Mark S." <m...@yahoo.comwrote:
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.

Jul 6 '07 #3

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Da***********@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
On Jul 6, 12:59 pm, "Mark S." <m...@yahoo.comwrote:
>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.

Jul 6 '07 #4

P: n/a
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark S." <ma***@yahoo.comwrote in message
news:eL****************@TK2MSFTNGP02.phx.gbl...
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

Jul 6 '07 #5

P: n/a
// this will run for a while...
int iUnique;
for (iUnique = int.MinValue; iUnique < int.MaxValue; ++iUnique)
{
// do something unique...
}
"Mark S." wrote:
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
Jul 6 '07 #6

P: n/a
Mark S. wrote:
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
Jul 6 '07 #7

P: n/a
On Jul 6, 10:59 am, "Mark S." <m...@yahoo.comwrote:
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

Jul 7 '07 #8

P: n/a
Hoss wrote:
On Jul 6, 10:59 am, "Mark S." <m...@yahoo.comwrote:
>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
Jul 7 '07 #9

P: n/a
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
Jul 7 '07 #10

P: n/a
Mark S. wrote:
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?
Still hard to squeeze 128 bit down into 64 bit.

Arne
Jul 7 '07 #11

P: n/a
good point. i'll see if the db guys can give up a bigger data type.
Jul 7 '07 #12

P: n/a
Mark S. wrote:
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
Jul 7 '07 #13

P: n/a
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.
"Mark S." <ma***@yahoo.comwrote in message
news:eL****************@TK2MSFTNGP02.phx.gbl...
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

Jul 8 '07 #14

P: n/a
That was what I suggested, but I guess i was too subtle. Apparenly everyone
else seems to think unique implies non-sequential...
"AlexS" wrote:
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.
"Mark S." <ma***@yahoo.comwrote in message
news:eL****************@TK2MSFTNGP02.phx.gbl...
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


Jul 9 '07 #15

P: n/a

P: n/a

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.

*** Sent via Developersdex http://www.developersdex.com ***
Sep 28 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.