def pivot_selection(rng, data, pivots, distance):
    """
    Function in charge of pivot selection.

    """
    b_prime = rng.randint(0, len(data) - 1)
    n = len(data)
    p = set()
    A = [0] * n
    D = []
    for i in range(pivots):
        maximum = 0
        b = b_prime
        D.append([0] * n)
        pivot = data[b]
        for j, item in enumerate(data):
            if j in p:
                continue
            d = distance(pivot, item)
            A[j] += d
            if A[j] > maximum:
                b_prime = j
                maximum = A[j]
        p.add(b_prime)
    p = list(p)
    for i in range(pivots):
        pivot = data[p[i]]
        for j, item in enumerate(data):
            D[i][j] = distance(pivot, item)
    return p, D
