The purpose of random.sample() is to randomly select a subset of the input sequence without selecting any items more than once. If there are no repetitions in your input sequence, you will not output either.
You are not looking for a subset; you need one random selection from the input sequence, repeated several times. Elements can be used several times. Use random.choice() in a loop for this:
for i in range(y): string = ''.join([random.choice(x) for _ in range(v)]) print string
This creates a string of length v , where characters from x can be used more than once.
Quick demo:
>>> import string >>> import random >>> x = string.letters + string.digits + string.punctuation >>> v = 20 >>> ''.join([random.choice(x) for _ in range(v)]) 'Ms>V\\0Mf| W@R ,#/.P~Rv' >>> ''.join([random.choice(x) for _ in range(v)]) 'TsPnvN&qlm#mBj-!~}3W' >>> ''.join([random.choice(x) for _ in range(v)]) '{:dfE;VhR:=_~O*,QG<f'
Martijn pieters
source share