I am looking for an efficient algorithm to get unique (non repeated) x amount of integer random numbers that are within the y and z limits.
e.g. I want x random integer numbers that are bigger or equal to y, smaller or equal to z and each random number is not repeated.
I came up with two solutions but none of them seems efficient to me.
One solution is on every time I get a random number from my generator I check if that number has been created before. If yes get a new one, if no add it in my list. This solution seems not efficient at all as the amount of needed numbers increases. Also it could theoretically keep looping indefinitely (although not probable).
Another solution I came up with is to keep all the possible numbers in a list and my generator will give me the position to remove a number from the list. Thus I will never have a duplicate number even if the generator will create the same number twice as the number that was there the first time in that position will be removed from the list. But this algorithm also does not seem efficient to me.
Is there a more efficient way to do this that guarantees completion and uniqueness of the random numbers generated?