Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python random.shuffle's randomness

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 function random().

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.

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!

like image 328
Bill Rong Avatar asked Jul 01 '10 17:07

Bill Rong


People also ask

Is Python random truly random?

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.

How do you shuffle randomly in Python?

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.

Why random shuffle is not working in 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.

How do you randomly shuffle a list with seeds in Python?

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.


1 Answers

You don't have anything to worry about. While under len(x) is under 2000, random.shuffle should work just fine.

like image 132
SilentGhost Avatar answered Oct 31 '22 23:10

SilentGhost