I want to generate an unique 4 digit random number. This is the below code what I have tried:
Code for generating random number
//Generate RandomNo
public int GenerateRandomNo()
{
int _min = 0000;
int _max = 9999;
Random _rdm = new Random();
return _rdm.Next(_min, _max);
}
The problem is I have received a random no with value 241
which is not a 4 digit number. Is there any problems with the code?
You can consider something like this.
The above code gives the result 0050.
Similarly you can try converting to string and verify.
you need a 4 digit code, start with 1000
0 is the same as 0000.
241 is the same as 0241.
You could format the integer to a string with a leading zero.
use: int _min = 1000;
or use leading 0 in case if you want 0241
I suggest to create new list and check if this list contains any of number