Python's random.shuffle limitation
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping
--
Chapters
00:00 Python'S Random.Shuffle Limitation
00:34 Answer 1 Score 2
00:52 Accepted Answer Score 5
01:50 Thank you
--
Full question
https://stackoverflow.com/questions/1066...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #algorithm #random
#avk47
ACCEPTED ANSWER
Score 5
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:
- Generate a list,
p, of all possible permutations of the input - Get a random number,
x, from your PRNG - The shuffled list is
p[x]
If p is longer than the list of all possible xs 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.
ANSWER 2
Score 2
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.