loop until u find non duplicate

I have this code:

chknext = Random.Range(0, 6);

int checkmonspawn()
    {
        getrannum = Random.Range(0, 4);

        if (getrannum == chcknext)
        {
            getrannum = Random.Range(0, 4);
        }
        return getrannum;

if getrannum and chcknext is the same, getrannum will get another random number. Problem is sometimes he gets the same number What is the efficient way to do this?

You can easily change if (getrannum == chcknext) to while (getrannum == chcknext). This will repeat the steps until not the same number will get generated. This is completely safe, because I can not even imagine 10 times in a row to get 2 generated. And we are talking about this being run on modern computers, which can do millions of computations a second.