From the Python documentation for random.shuffle, which takes a list and randomizes the order if its elements:
Note that for even rather small len(x), the total number of permutations of x is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.
Is this true of any language, since the limitation seems dependent on the random number generator? Is it possible to write a function that can generate any possible permutation of an arbitrarily long list?
Python Random shuffle() Method The shuffle() method takes a sequence, like a list, and reorganize the order of the items. Note: This method changes the original list, it does not return a new list.
One of the easiest ways to shuffle a Pandas Dataframe is to use the Pandas sample method. The df. sample method allows you to sample a number of rows in a Pandas Dataframe in a random order. Because of this, we can simply specify that we want to return the entire Pandas Dataframe, in a random order.
In Python, you can shuffle (= randomize) a list, string, and tuple with random. shuffle() and random. sample() . random.
Use the Random seed and choice method together The random choice() function is used to choose a random element from the list and set. By setting the custom seed value, you can pick the same choice every time.
See http://mail.python.org/pipermail/python-ideas/2009-March/003670.html . The exact length that it starts being a problem at is dependent on the PRNG, but the basic problem will always apply.
The previous question that @TryPyPy linked to is also focused on Python, but the accepted answer explains quite well why it will always happen. To paraphrase, you can imagine a naive shuffle algorithm working this way:
p
, of all possible permutations of the inputx
, from your PRNGp[x]
If p
is longer than the list of all possible x
s that the PRNG can produce, some of the permutations are unreachable.
Since fancy shuffle algorithms are, at their core, a way of doing this without having to generate every possible permutation before discarding all but one of them, the only way around this is to have a source of true randomness.
Yes, it is possible. You can write a permutation generator that uses random.SystemRandom
for all of your decisions.
The downside of this is that your program may have to halt for an arbitrarily long time while your operating system collects more entropy for you to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With