1 | initial version |
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(5^2)$ 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)
.
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(5^2)$ 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)
.