How to Generate Unique Number in C# of 8 digits?

A GUID is not just a random number; it's composed of segments. Some of the segments will not change at all if the guid is generated on the same computer. By using only 64-bits of the original 128-bits you are breaking the structure of the guid and most likely breaking the uniqueness of the generated number This question has more info on uniqueness of guids, check this link as well for more info on why it's bad to use only part of a guid if you need a unique number If you need to limit duplication to an absolute minimum, an incremental counter will give you what you need.

If your application is using multiple threads or processes, a counter may be hard (or even impossible) to implement correctly This is the realm that guids were designed for, to be unique across multiple machines. So if uniqueness across machines is a requirement you should use guids. The whole guid.

You'll hit the same random number again sooner than just counting from 0 to 99999999. – Vladislav Zorov May 23 at 22:40 1 That's not exactly right. Version 4 GUIDs (which are generated by Guid.NewGuid()) have exactly 6 bits fixed, the rest is pseudo-random.

The old version of GUIDs behaved the way you describe. – svick May 23 at 22:44 I won't vote this down, but a random number generator will still yield duplicates. Either you need a function that guarantees non-duplicate enumeration of all values, or you need memory.

– Kevin Hsu May 23 at 23:12.

Any random sequence is bound to have some collisions. It's just a matter of when. Using the birthday paradox formula, with 100,000,000 possible values (8 digits), the chance that you will have a collision with only 10,000 elements is around 40% and 99% with 30,000 elements.(see here for a calculator).

If you really need a random sequence, you should not use a GUID for this purpose. GUIDs have very specific structure and should only be taken as a whole. It is easy enough to create a random 8 digit sequence generator.

This should give you an 8 digit sequence: public string Get8Digits() { var bytes = new byte4; var rng = RandomNumberGenerator.Create(); rng. GetBytes(bytes); uint random = BitConverter. ToUInt32(bytes, 0) % 100000000; return String.

Format("{0:D8}", random); } You can also take the RandomNumberGenerator and place it somewhere to avoid creating a new one everytime.

My first answer did not address the uniqueness problem. My second one does: static int counter; public static int GetUniqueNumber() { return counter++; } If you want to have unique numbers across app restarts, you need to persist the value of counter to a database or somewhere else after each and every GetUniqueNumber call.

1 Use Interlocked. Increment(1) – Hans Passant May 23 at 23:26.

You can do this: Random rand = ...; return rand. Next(10000000, 100000000).ToString().

There's no such function as GetNext on the Random class - I think you mean Next - this isn't guaranteed to generate a unique sequence of integers, in fact, I'd expect it to be rather non-unique fairly quickly. – Will A May 23 at 22:31 It will start generating non-unique numbers after about 60000 numbers generated. You are right, I missed the "unique" requirement of the questions.

– usr May 23 at 22:36.

The range of values is too small. An incrementing counter is the best solution, like in an ERP system - you set the first customer number to 1000 and the next one is 1001, 1002,...,99999999. Otherwise, if you get a random number (or part of GUID) from these, you'll hit the same number again.

Depending on your app, sooner or later but it's guaranteed to happen sooner than just iterating over them.

If you want a unique number between 10000000 and 99999999, start an integer from 10000000 and just start incrementing it. Generating sequentially ordered numbers is no less random than any other generated sequence, and a whole lot easier to generate.

A GUID is not just a random number; it's composed of segments. Some of the segments will not change at all if the guid is generated on the same computer. By using only 64-bits of the original 128-bits you are breaking the structure of the guid and most likely breaking the uniqueness of the generated number.

This question has more info on uniqueness of guids, check this link as well for more info on why it's bad to use only part of a guid if you need a unique number. If you need to limit duplication to an absolute minimum, an incremental counter will give you what you need. If your application is using multiple threads or processes, a counter may be hard (or even impossible) to implement correctly.

This is the realm that guids were designed for, to be unique across multiple machines. So if uniqueness across machines is a requirement you should use guids. The whole guid.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions