lucycopp lucycopp - 3 months ago 7
C# Question

C# needing a six digit number beginning with two 0's

I'm making a program where products are searched for by there batch number. I want to the batch number to be a random 6 digit code starting with 00, for example 002142; at the moment it is only making a random 6 digit code.
Here is the code I have tried:

public string getRandomNumber()
{
Random random = new Random(100000);
string randomNumber = random.Next(999999).ToString("D6");
return randomNumber;
}

Answer

why is everybody creating 6 digit ? what really is needed is create 4 and concat.

int random = new Random().Next(1000, 9999);
string code = "00" + random.ToString();

EDIT:

Thanks for marking as correct answer but my code is wrong. If you don't pass minimum value to next method, you can get numbers from 1 to 999 which you don't want. I edited my answer. Hope it's not late for anything.

Comments