Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate Unique Number of 8 digits?

Tags:

c#

numbers

unique

I am using this code to generate a 8 digit unique number.

byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToUInt32(buffer, 8).ToString();

Does this code really generate a unique number or might it repeat the same number again?

like image 383
dev Avatar asked May 23 '11 22:05

dev


People also ask

How many unique numbers are there in 8 digits?

How many 8-digit numbers are there in all? Therefore, 90000000 eight digit numbers are there in all.

How do I generate a random 8-digit number in Excel?

Type =RAND() and press Enter. The RAND function takes no arguments. 3. To generate a list of random numbers, select cell A1, click on the lower right corner of cell A1 and drag it down.

How do I find a unique number?

The number will be unique if it is positive integer and there are no repeated digits in the number. In other words, a number is said to be unique if and only if the digits are not duplicate. For example, 20, 56, 9863, 145, etc. are the unique numbers while 33, 121, 900, 1010, etc.

Who is 8-digit number?

The greatest 8-digit number is 99,999,999. The smallest 6-digit number is 100,000. Their difference is 99,999,999 - 100,000 = 99,899,999.


1 Answers

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.

like image 80
Marnix van Valen Avatar answered Oct 04 '22 04:10

Marnix van Valen