ksg ksg - 3 months ago 45
C# Question

Unique 4 digit random number in c#

I want to generate an unique 4 digit random number. This is the below code what I have tried

Code for generating random no

//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 problem with the code.

Any help will be highly appreciated.

Answer
//Generate RandomNo
    public int GenerateRandomNo()
    {
        int _min = 1000;
        int _max = 9999;
        Random _rdm = new Random();
        return _rdm.Next(_min, _max);
    }

you need a 4 digit code, start with 1000