Shuffle a long list even more times in Python

I want to shuffle a long sequence (say it has more than 10,000 elements) many times (say 10,000). While reading the Python Random documentation, I found the following:

Note that even for small len (x), the total number of permutations x can grow rapidly more than the period of most random number generators. This means that most permutations of a long sequence can never be generated. For example, a sequence of length 2080 is the largest that can fit into the period of the Mersenne Twister random number generator

I have two groups (maybe more), and each has many meanings. The sequence I want to shuffle is a list of all available values, regardless of group. My concern is that the note implies that the shuffle I need may not be provided by the random.shuffle () function.

I thought of some workarounds:

  • Initialize a random number generator (with random.seed ()) several in certain iterations. Thus, it does not matter if the permutations are more than a period, because different seeds will get different results.
  • Use a pattern (range (sequence length), k = group size) to get random indexes and then use them to index inside each group. Thus, I cannot get out of permutations due to the period of the random number generator.

Did any of my alternatives help?

Thanks a lot!

+5
source share
2 answers

Well 10,000! ~= 10^36,000 10,000! ~= 10^36,000 These are many possible permutations. The best you can do is to understand how your operating system or hardware accumulates “really random” bits. You could expect ~ 120,000 bits of randomness that you're fine, and then use an algorithm that generates the nth permutation of your input list, given random n.

+1
source

You can use the shuffle numpy function to shuffle the list items in place

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

Random Call Dates (in Jupyter)

 %timeit np.random.shuffle(L) 

You are getting

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

Source: https://habr.com/ru/post/1272789/


All Articles