473,661 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does this code generate unique integers?

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
16 6913
Guid == 128 bits
Guid.GetHashcod e() == 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.co mwrote in message
news:eL******** ********@TK2MSF TNGP02.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
On Jul 6, 12:59 pm, "Mark S." <m...@yahoo.com 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
No, it does not. You need to use the Random class.

Jul 6 '07 #3
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.co m

<Da***********@ gmail.comwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
On Jul 6, 12:59 pm, "Mark S." <m...@yahoo.com 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(Syste m.Guid.NewGuid( ).GetHashCode() )

TIA

No, it does not. You need to use the Random class.

Jul 6 '07 #4
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.co m

"Mark S." <ma***@yahoo.co mwrote in message
news:eL******** ********@TK2MSF TNGP02.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
// 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
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
On Jul 6, 10:59 am, "Mark S." <m...@yahoo.com 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
Just interpret the bytes that represent the guid as an integer

Jul 7 '07 #8
Hoss wrote:
On Jul 6, 10:59 am, "Mark S." <m...@yahoo.com 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?
Just interpret the bytes that represent the guid as an integer
C# does not have 128 bit integers ...

Arne
Jul 7 '07 #9
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,8 54,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

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

Similar topics

3
1584
by: S Kemplay | last post by:
Hi all, I wrote a script to choose random dates for a statistics assignment. I only need to choose 30 dates from one year with no leap years and it works fine. However I tested with different numbers of dates. It hangs from 450up. I only need 30 dates but it would be good to know why it hangs. (My coding probably has something to do with it :)) import random
8
4476
by: Joe Wong | last post by:
Hi, I need to implement a unique number generator that 1 or more processes on same or different machines will make use of it. Is there any library / project available already for this? Thanks in advance, -- Wong
10
18587
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from tablename; Here this function generate unique id for each row of the table. Regards..
2
4535
by: Chris Dunaway | last post by:
I have a web service which is accessed by a windows forms application. When the application submits a unit of work (a "job"), I want to return a job receipt or tracking number back to the application. My requirements are these: 1. The receipt number must be unique (like a guid is unique) 2. The receipt must be NON sequential. 3. If possible, I'd like it to be 10 to 12 characters long. 4. If possible, I'd like to include only...
8
13375
by: mortb | last post by:
Hi, How do I write a GenerateHashcode function that will generate guaranteed unique hashcodes for my classes? cheers, mortb
45
3392
by: salad | last post by:
I'm curious about your opinion on setting relationships. When I designed my first app in Access I'd go to Tools/Relationships and set the relationships. Over time I'd go into the window and see relationship spaghetti....tables/queries all overthe place with lots of relationship lines between here and there. After that first app I didn't do relationships. If I had a query, I defined the relationship. Many of the times when I create a...
14
4842
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
25
2007
by: Tom | last post by:
I need to convert an integer to a unique code string consisting only of capital letters and numbers. I also need to be able to convert it back to an integer again. Example: 10 =DFG56JKG4 =10 How do I accomplish that? Googling has not helped.
15
37581
by: Ashish Khandelwal | last post by:
As MSDN is not giving us guarantee upon uniqueness of Hash Code, so could any one suggest me that how to generate a unique Hash Code for same string always, and generate different-2 Hash Code Different-2 string.
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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

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