Kusuma585539

Hey,

In our application, I want to generate 8 characters hexadecimal unique value always.

I tried with GUID.gethascode.Tostring("x") but sometimes even it creates dupilcates.

It should generate unique always.

Thanks

Kusuma




Re: Visual C# General Unique value generation?

Matthew Watson

Define Unique:

* For this run of the program
* For this computer
* For all computers on this network
* For all computers in the world





Re: Visual C# General Unique value generation?

Kusuma

I need unique key for all the computers in the world.

Thanks

Kusuma






Re: Visual C# General Unique value generation?

Matthew Watson

8 hex characters is basically an Int32.

It's going to be really hard to ensure that you can generate a unique 32-bit number for all computer. In fact, the only way you could possibly do it is to have some central site on the internet which generates new numbers on request.

Other than that, you will need to go to something more than an int32.

A network card's MAC address is supposed to be unique - they are 48-bits. But even they are not guaranteed to be unique, since some devices allow MAC-spoofing.

The obvious solution is to use a Guid, but you will already have considered and rejected that.

So I'm afraid that the only solution, if you MUST only use 32 bits, is to have a central site on the internet to generate the numbers.




Re: Visual C# General Unique value generation?

nielsvanvliet

I agree with Matthew Watson.

The following code produces:
- produce unique numbers during one session of the application.
- has a low probility to produce the same numbers if ran accross different application.

Note that the class Guid is the standard tool in .Net to have a low probability to have twice the same result. Also, I advice to retrieve 64bits (16 hexa nbr) to reduce the probability (or to reduce it to zero using a global worldwide counter, which is difficult).

The numbers are not 'randomly' distributed (I focused on unique):
Code Snippet

public static class Unique
{
static UInt32 cpt;
static UInt32 init;
static Unique()
{
byte[] b = Guid.NewGuid().ToByteArray();
cpt = init = ((UInt32)b[0] ^ (UInt32)b[1]) +
(((UInt32)b[2] ^ (UInt32)b[3]) << 8) +
(((UInt32)b[4] ^ (UInt32)b[5]) << 16) +
(((UInt32)b[6] ^ (UInt32)b[7]) << 24);
}
static public string Next()
{
if (++cpt == init)

{--cpt; throw new Exception("Can not produce a new one.");}
return cpt.ToString("x");
}
}







Re: Visual C# General Unique value generation?

Kusuma

Hey nielsvanvliet amd Matthew,

Thanks for the solution.

Niel.

I read in one document about GUID "some times even it creates duplicate values .But you are saying "Guid is the standard tool in .Net to have a low probability to have twice the same result"

My problem is "Our application will be installed in server and have access to so many computers with in network .when ever the user accesses this application in network it should create an unique hexadecimal value.

Note:The user can can access any number of times this application.All the time the application has to create a unique value.

Please let me ,whats the solution for this problem

Thanks

Kusuma






Re: Visual C# General Unique value generation?

Matthew Watson

The solution, as we already said, is to use a Guid. That is precisely what they are for.

Ignore anyone who says that you can get two Guids the same. The chances of that happening are so close to zero as to be indifferentiable.





Re: Visual C# General Unique value generation?

Kusuma

Thanks Mathhew :-)

Kusuma






Re: Visual C# General Unique value generation?

nielsvanvliet

If you rely on the GUID to be unique, I advice you to use 16 hexa numbers, and not 8.

If you use 16 hexa numbers, the documentation of MS specifies that the probability is very low. So as to say that, they have matematicians which proved that the probability is low. If you use 8 of them (32 bits) and combine them (the best way is to use xor, as I did in my code above), the proof does not apply. Also note that by using 2 times less bits (32 less bits), you do not divied the probability by 2, and not even by 2^32, but much more.

Personnaly, I would:

- not do it on 32 bits even if it is not crusial

- I would trust 64 bits, if it an error can not put my company in any trouble.






Re: Visual C# General Unique value generation?

Matthew Watson

If it ain't 128-bits, it ain't a Guid. Smile

So I wouldn't even chance using 64 bits - I'd use the entire 128 bits.

Ok, so that's 16 bytes - but it's what it takes to ensure uniqueness!