'How can I make sure my RNG numbers are unique?

I'm trying to select 2 random items out of a list using the RNG class. The problem is occasionally I get the same 2 numbers and I'd like them to be unique. I tried using a while loop to get another number if the it's the same as the last one but adding even a simple while loop results in an "Exceeded prepaid gas" error. What am I not understanding?

//simplified for posting question
var lengthOfList = 10
var numItemsWanted = 2
//Get more rng numbers than I need incase of duplicates
const rng = new RNG<u32>(lenghtOfList, lengthOfList)

for(let i = 0; i < numItemsWanted; i++) {
    var r = rng.next()
    while (r == rng.last()) {
        r = rng.next()
    }
    newList.push(oldList[r])
}

Working:

//simplified for posting question
var lengthOfList = 10
var numItemsWanted = 2
//Get more rng numbers than I need incase of duplicates
const rng = new RNG<u32>(lenghtOfList, lengthOfList)
let r = rng.next()
let last = r + 1
for(let i = 0; i < numItemsWanted; i++) {
    newList.push(oldList[r])
    last = r
    r = rng.next()
    while (r == last) {
        r = rng.next()
    }
}



Solution 1:[1]

If you remove the item from oldList once picked, it would be imposible to picked it again.

Another aproach is to shuffle your oldList and then pick the first two items.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 asceta