I need to generate 8 digit unique id in c#. On my site a user will register and I need to generate a unique id for him in c# code(I don't want this logic in DB), after inserting the id I need to save it in database.
Edit: I need that numbers to be generated in random manner everytime.
Random rnd = new Random(); int myRandomNo= rnd. Next(10000000, 99999999); // creates a 8 digit random no.
The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.
Although not 8 digits, I'd use a GUID for that purpose:
var id = Guid.NewGuid().ToString()
From Wikipedia:
Ideally, a GUID will never be generated twice by any computer or group of computers in existence. The total number of unique keys (2^128 or 3.4×10^38 - in relation there are about 1.33×10^50 atoms on earth) is so large that the probability of the same number being generated twice is extremely small, and certain techniques have been developed to help ensure that numbers are not duplicated
If you don't mind the IDs being predictable, I'd go with Vlad's suggestion.
Otherwise, I'd generate a random number in the required range and just try to insert it in the database... if you get an exception due to the uniqueness constraint being violated in the database (and that constraint absolutely should be there) then try again. Keep trying until it either works or you've gone round a certain number of times. (It's highly unlikely that you'll fail 100 times for example - unless you've got a bug elsewhere, in which case an exception is preferable to an infinite loop.)
So this isn't generating the ID in the database - but it's verifying the uniqueness in the database, which is after all the ultimate "source of truth".
If you don't need cryptographically securely generated IDs, then simply using Random.Next(100000000)
will generate you a value in the range [0, 99999999]. If you don't want any values which need leading 0s to get to 8 digits, simply use Random.Next(10000000, 100000000)
which will give you a smaller range of possible values, but you won't need to worry about them having fewer than 8 digits.
Using Random
properly has a few "gotchas" - see my article about it for more details.
You could try to implement a method that generates a random number but you have always to check if it is already in database.
static void Main(string[] args)
{
HashSet<string> numbers = new HashSet<string>();
for (int i = 0; i < 100; i++)
{
numbers.Add(GenerateRandomNumber(8));
}
Console.WriteLine(numbers.Count == 100);
Console.ReadLine();
}
static Random random = new Random();
static string GenerateRandomNumber(int count)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i++)
{
int number = random.Next(10);
builder.Append(number);
}
return builder.ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With