Question: As the title of this post, I found the answer regarding "Can we put subviews of a UIView in front of its CALayer" in [this](https://stackoverflow.com/questions/6551394/put-subviews-of-a-uiview-in-front-of-its-calayer) post, but I cannot find the answer to *why* this is so. In my opinion, having the view as the subview of the layer's view only suggests "stronger" order between the subview and the layer. Can somebody explain this to me?

Answer: 
Here is an approach which works by taking a number in the range `0 to n*(n-1)/2 - 1` and decodes it to a unique pair of items in the range `0 to n-1`. I used 0-based math for convenience, but you could of course add 1 to all of the returned pairs if you want:

```
import math
import random

def decode(i):
    k = math.floor((1+math.sqrt(1+8*i))/2)
    return k,i-k*(k-1)//2

def rand_pair(n):
    return decode(random.randrange(n*(n-1)//2))

def rand_pairs(n,m):
    return [decode(i) for i in random.sample(range(n*(n-1)//2),m)]

```

For example:

```
>>> >>> rand_pairs(5,8)
[(2, 1), (3, 1), (4, 2), (2, 0), (3, 2), (4, 1), (1, 0), (4, 0)]

```

The math is hard to easily explain, but the `k` in the definition of `decode` is obtained by solving a quadratic equation which gives the number of [triangular numbers](https://en.wikipedia.org/wiki/Triangular_number) which are `<= i`, and where `i` falls in the sequence of triangular numbers tells you how to decode a unique pair from it. The interesting thing about this decode is that it doesn't use `n` at all but implements a one-to-one correspondence from the set of natural numbers (starting at 0) to the set of all pairs of natural numbers.
I don't think any thing on your line can improve. After all, as your `m` get closer and closer to the limit `n(n-1)/2`, you have thinner and thinner chance to find the unseen pair.

I would suggest to split into two cases: if `m` is small, use your random approach. But if `m` is large enough, try

```
 pairs = list(itertools.combination(buffet,2))
 ponits = random.sample(pairs, m)

```

Now you have to determine the threshold of `m` that determines which code path it should go. You need some math here to find the right trade off.