I want to create a list and fill it with 15 zeros, then I want to change the 0 to 1 in 5 random spots of the list, so it has 10 zeros and 5 ones, here is what I tried
import random, time
dasos = []
for i in range(1, 16):
dasos.append(0)
for k in range(1, 6):
dasos[random.randint(0, 15)] = 1
Sometimes I would get anywhere from 0 to 5 ones but I want exactly 5 ones, if I add:
print(dasos)
...to see my list I get:
IndexError: list assignment index out of range
randrange(0, 15)orrandint(0, 14)random.sample(range(15), 5)will give you 5 unique numbers between 0 and 14.canditates = range(15)), shuffle this list withrandom.shuffle(candidates), and then use the first 5 numbers as your indices.