Why am I getting dups with random.shuffle in Python? -


For a list of 10 ints, there are 10! Why possible order or permutation is random after only 5000 attempts? Does Schlämt duplicate?

  & gt; & Gt; & Gt; L = class (10) & gt; & Gt; & Gt; RL = list ()> & Gt; & Gt; In the I category (5000): ... random. Shuffle (L) ... RL.Append (L [:]) ... & gt; & Gt; & Gt; RL = [tl (e)]> gt; & Gt; & Gt; Lane (Set (RL)) 4997 & gt; & Gt; & Gt; I, for EN enumerate (RL) t: ... if RL Cotet (T)> 1: ... printed, t ... 102 (7, 5, 2, 4, 0, 6, 9, 3, 1, 8) 258 (1, 4, 0, 2, 7, 3, 5, 9, 6, 8) 892 (1, 4, 0, 2, 7, 3, 5, 9, 6, 8) 2878 (7, 5, 2, 4, 0, 6, 9, 3, 1, 8) 4123 (5, 8, 0, 1, 7, 3, 2, 4, 6, 9) 4633 (5, 8, 0, 1, 7, 3, 2, 4, 6, 9) & gt; & Gt; & Gt; 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 3628800 & gt; & Gt; & Gt; 2 ** 19937 - 1 431542479738816264805523551633791983905393 [snip]> gt; & Gt; L = list ()> & Gt; & Gt; In the I category (5000): ... el.appand (random.choice (xrange (3628800)) ... & gt; & Gt; & Gt; Lane (Set (L)) 4997  

EDIT: FWI, if there is a possibility of not having two for a single pair: p = (10! - 1) / 10! And the number of coincidences is: C = 5000! / 49 9 8! * 2! = 5000 * 4999/2 is likely to be a duplicate:

  gt; & Gt; Import Mathematics & gt; & Gt; & Gt; F = math.factorial (10) & gt; & Gt; & Gt; P = 1.0 * (F -1) / F> & Gt; & Gt; C = 5000.0 * 4999/2> gt; & Gt; 1 - P ** C 0.96806256495611798  

It is called.

According to this formula Wikipedia:

but with 365 with 10! To change you only need 2200 instances, so that there is a 50% chance of collision, and you are above top.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -