Why not use shuffle()? On Monday 04 Mar 2013, Simon King wrote: > Hi Gary, > > On 2013-03-04, GaryMak <garymako...@googlemail.com> wrote: > >> sage: Permutations(50).random_element() > >> [11, 4, 30, 48, 49, 36, 22, 16, 27, 6, 44, 33, 13, 50, 9, 35, 15, 12, > >> 26, 45, 1, 18, 2, 40, 19, 10, 28, 7, 37, 46, 25, 29, 34, 41, 38, 24, 8, > >> 20, 32, 21, 14, 23, 31, 47, 43, 17, 3, 39, 5, 42] > > > > I have a set of size (say) 50 and I would like to generate a > > randomly-ordered list of its elements. The trouble is, if I use > > Permutations(my_set).random_element() it seems to want to generate the > > whole lot of them before picking out a random one!! Sorry but I cannot > > find how else to invoke this functionality, but I know there must be a > > better way .... > > Permutations(50).random_element() is quite fast, and I think it does > *not* try to enumerate Sym(50). > > I'd do: > sage: L = some list > sage: p = Permutations(len(L)).random_element() > sage: L_rand = [L[p[i]-1] for i in range(len(L))] > > Note that it is p[i]-1, because addressing list elements in Python is > zero-based, but Permutations(50) permutes the numbers from 1 to 50, not > from 0 to 49. > > Best regards, > Simon
Cheers, Martin -- name: Martin Albrecht _pgp: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x8EF0DC99 _otr: 47F43D1A 5D68C36F 468BAEBA 640E8856 D7951CCF _www: http://martinralbrecht.wordpress.com/ _jab: martinralbre...@jabber.ccc.de -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support?hl=en. For more options, visit https://groups.google.com/groups/opt_out.