3 ms·
If you want to do it the other way around, you have to start at 0: for i in range(len(x) - 1): r = randrange(i, len(x)) x[i], x[r] = x[r],
by chkas 4y ago
If you want to do it the other way around, you have to start at 0:
for i in range(len(x) - 1):
r = randrange(i, len(x))
x[i], x[r] = x[r], x[i]
- jwilk 4y agoThat probably works too, but it's even more awkard than the Knuth's one, and doesn't have the nice properties I mentioned. I meant just: for i in range(1, len(x)): r = randrange(i + 1) x[i], x[r] = x[r], x[i]