Click here to Skip to main content
15,746,534 members
Please Sign up or sign in to vote.
2.14/5 (4 votes)
See more:
How go I generate a 4 digit number starting from 0000 to 4999.
This is what I've tried so far
Random rnd = new Random();
rnd.Next(0000, 4999);
int id = rnd;

I can use (1, 9) four times but isn't there another way?
Updated 9-Aug-13 9:19am
Sergey Alexandrovich Kryukov 9-Aug-13 15:22pm    
The question makes no sense at all.
If you really need to have them from 0 to 4999, do this. Do you really understand that 0, 00, 000, and 0000 is the same number?
Your second options really means "all numbers between 0 and 4999 having no zeros in decimal representation of numbers".
I don't think that you really mean it. I'm almost sire you don't need such a set. If you do, you don't need another way.
Anyway, if you have such a weird requirement, explain why.
You have one fundamental problem: cannot formally specify requirements. How we can seriously talk about any programming then?
Member 7434378 9-Aug-13 15:32pm    
I don't want to start from 1000 I want to start from 0, e.g if I want the number 3 it must be like 0003.
Manfred Rudolf Bihy 9-Aug-13 16:12pm    
A number is a number, but what you're talking about has to do how to format the number when ouput. Please see here:

So to solve your question all you have to do is generate a number between 0 and 4999 and then Format it with D4 as the format specification. This will pad the output with zeros up to a length of 4 characters.

Matt T Heffron 12-Aug-13 15:21pm    

Try this:

Random rnd = new Random();
int myRandomNo = rnd.Next(1000, 9999);
Share this answer
Manfred Rudolf Bihy 9-Aug-13 16:44pm    
OP has a formatting problem and not a problem with generating the numbers themself. In my comment to OP's question I advised him to use String.Format("{0:D4}", number) to solve this issue.
Manas Bhardwaj 9-Aug-13 16:49pm    
Hi Manfred,

If I see OPs code i.e.

rnd.Next(0000, 4999);

This would not always generate a 4 digit number as the seed starts from 0. Potentially it can generate numbers from 0 - 999.

Adding to that, I am not sure why OP stops at 4999 when he can go on to 9999.
Manas Bhardwaj 9-Aug-13 16:50pm    
Ah, I see the discussion thread now. It was not at least clear to me from the original question. :)
Random rnd = new Random();
int id = rnd.Next(0, 4) + rnd.Next(0, 9) + rnd.Next(0,9) + rnd.Next(0, 9); 
Share this answer
ridoy 9-Aug-13 15:57pm    
this is not a definitely not random number.
Manfred Rudolf Bihy 9-Aug-13 16:01pm    
Sorry, but that is not a solution to your question. I hope you're not trying to abuse CP here. You are just adding these numbers up.
Manas Bhardwaj 9-Aug-13 16:36pm    
This would maximum be 2 digit number (25).
Manas Bhardwaj 9-Aug-13 16:36pm    
Btw, look at my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900