import secrets


def random_split(s, d, n):
    """Split each secret given in s into n random Shamir shares.

    The (maximum) degree for the Shamir polynomials is d, 0 <= d < n.
    Return matrix of shares, one row per party.
    """
    p = s[0].modulus
    m = len(s)
    shares = [[None] * m for _ in range(n)]
    for h in range(m):
        c = [secrets.randbelow(p) for _ in range(d)]
        # polynomial f(x) = s[h] + c[0] x + c[1] x^2 + ... + c[d-1] x^d
        for i in range(n):
            y = 0
            for c_k in c:
                y += c_k
                y *= i + 1
            shares[i][h] = (y + s[h].value) % p
    return shares