Processing math: 100%
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 1 year ago

Max Alekseyev gravatar image

If under the hamming weight you understand the number of nonzero components, then something along these lines will do the job:

def random_vector(q,n,w):
    F = GF(q)
    S = Subsets(range(n),w).random_element()
    v = vector(F,n)
    for s in S:
        while True:
            v[s] = F.random_element()
            if v[s]:
                break
    return v

Here random_vector(5^2,10,3) returns a random vector over GF(52) with 3 nonzero out of total 10 components, such as (0, 0, 2*z2 + 3, 0, 4*z2 + 1, 0, 0, 4*z2 + 1, 0, 0).

click to hide/show revision 2
No.2 Revision

If under the hamming weight you understand the number of nonzero components, then something along these lines will do the job:

def random_vector(q,n,w):
    F F.<b> = GF(q)
GF(q, modulus='primitive')
    S = Subsets(range(n),w).random_element()
    v = vector(F,n)
    return vector([b^randint(0,q-2) if k in S else F.zero() for s k in S:
        while True:
            v[s] = F.random_element()
            if v[s]:
                break
    return v
range(n)])

Here random_vector(5^2,10,3) returns a random vector over GF(52) with 3 nonzero out of total 10 components, such as (0, 0, 2*z2 + 3, 0, 4*z2 + 1, 0, 0, 4*z2 + 1, 0, 0).