Following is from python website, about
random.shuffle(x[, random])
Shuffle the sequence
x
in place. The optional argument random is a 0-argument function returning a random float in[0.0, 1.0)
; by default, this is the functionrandom()
.Note that for even rather small
len(x)
, the total number of permutations ofx
is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.
If I want to repeat getting a random permutation of ['a'..'k']
, it seems shuffle will NOT give me the randomness. Is my understanding right?
Thank you!
Most random data generated with Python is not fully random in the scientific sense of the word. Rather, it is pseudorandom: generated with a pseudorandom number generator (PRNG), which is essentially any algorithm for generating seemingly random but still reproducible data.
shuffle() function in Python. The shuffle() is an inbuilt method of the random module. It is used to shuffle a sequence (list). Shuffling a list of objects means changing the position of the elements of the sequence using Python.
You will get an error if you try to shuffle a string using the shuffle() method. Because a string is an immutable type, and You can't modify the immutable objects in Python. The random. shuffle() doesn't' work with String.
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.
You don't have anything to worry about. While under len(x)
is under 2000, random.shuffle
should work just fine.
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