import random
size = 100
a = list(range(size))
def rng_swap(a,i):
rng = random.randint(0,len(a)-1)
a[i],a[rng] = a[rng],a[i]
def kindof_shuffle(a,swap_chance):
for i in range(len(a)):
if random.random()<0.1:
rng_swap(a,i)
kindof_shuffle(a,swap_chance=0.2)
print(a)
print(sum([v==i for i,v in enumerate(a)])/size)
should leave about 80% (+/- some) of the array unchanged
or you could do something like to get closer to exactly 20%
twnty_pct = int(size*0.2)
indices = random.sample(list(range(size)),twnty_pct)
for i1,i2 in zip(indices,indices[1:]):
a[i1],a[i2] = a[i2],a[i1]
print(a)
note that the second solution is suboptimal and includes some extra swaps that are not really adding much