No. It is not guaranteed and guaranteeing that would make it less secure because it could be possible for an attacker to exploit the knowledge that you will never get the same number on successive calls. It is completely possible for a random sequence of numbers to contain "runs" of numbers. If you have a list of numbers out of which you have an equal chance of picking any number, then it is very possible that you can pick the same number twice. Of course, the probability of that happening is lower than the probability of getting a sequence of two different numbers, but it is not zero.
Also, one of the requirements for a CSPRNG is that given a state, you should not be able to predict the next state (i.e., all numbers in your range have an equal probability of showing up). But with your requirement, you can predict that if your random number generates a number x
in the range 0
to n
, then in the next call you will get any number but x
.
While there may be some sort of state-machine inside the CSPRNG, each call is essentially independent of previous calls and should not be affected by them.
If you're going for uniqueness, you should use something like a UUID. These can be generated randomly with the advantage that you get uniqueness (if you are using a CSPRNG and the seed source has sufficient entropy).
If the requirement is that the client doesn't get two identical random numbers, then the code is not redundant. But it will violate the "randomness" property; how important that is, depends on your particular situation.