There's a function called random.sample()
which, given a sequence of items, will give you back a certain number of them randomly selected.
import random
for index in random.sample(xrange(len(vector)), y): # len(vector) is "x"
vector[index] = 1
Another way to do this would be to combine a list of x
0's and y
1's and shuffle it:
import random
vector = [0]*(x-y) + [1]*y # We can do this because numbers are immutable
vector.shuffle()