Skip to content Skip to sidebar Skip to footer

Shuffle A Long List An Even Longer Number Of Times In Python

I want to shuffle a long sequence (say it is has more than 10000 elements)a lot of times (say 10000). When reading Python Random documentation, I found the following: Note that e

Solution 1:

Well 10,000! ~= 10^36,000 That is a lot of possible permutations. The best you could do is to delve into how your operating system or hardware accumulates "truly random" bits. You could then wait for ~120,000 bits of randomness that you are OK with then use the algorithm that generates the n'th permutation of your input list given that random n.

Solution 2:

You can use numpy shuffle function to shuffle the list elements in-place

import numpy as np

L = range(0, 10000)
np.random.shuffle(L)

Timing the shuffle call (in Jupyter)

%timeit np.random.shuffle(L)

you get

10000 loops, best of 3: 182 µs per loop

Post a Comment for "Shuffle A Long List An Even Longer Number Of Times In Python"