Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45876
| Date | 2013-05-24 07:26 -0400 |
|---|---|
| From | Ned Batchelder <ned@nedbatchelder.com> |
| Subject | Re: Simple algorithm question - how to reorder a sequence economically |
| References | <a31c773f-88d8-4489-8640-12123d884e4d@m2g2000vbb.googlegroups.com> <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2055.1369394778.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On 5/24/2013 6:52 AM, Steven D'Aprano wrote: > On Fri, 24 May 2013 01:14:45 -0700, Peter Brooks wrote: > >> What is the easiest way to reorder a sequence pseudo-randomly? > import random > random.shuffle(sequence) > > > The sequence is modified in place, so it must be mutable. Lists are okay, > tuples are not. > > >> That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg >> 2,1,4,3) that is different each time. > You can't *guarantee* that it will be different each time. With a four- > item list, there are only 4! = 24 combinations, so on average you'll get > the original order one time in 24. For a ten-item list, that is once > every 3628800 times, and for a twenty-item list, once in > 2432902008176640000 times. But of course these are *random*, and there's > always a chance of this: > > http://dilbert.com/strips/comic/2001-10-25 > > Also, heed the note in the docs: "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." The default random number generator has a period of 2**19937-1, which means that lists longer than 2080 have more permutations than the period of the generator (because factorial(2081) > 2**19937). Most shuffles involve lists far shorter than 2080, but it's still good to keep it in mind. --Ned.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 01:14 -0700
Re: Simple algorithm question - how to reorder a sequence economically Chris Angelico <rosuav@gmail.com> - 2013-05-24 18:37 +1000
Re: Simple algorithm question - how to reorder a sequence economically Fábio Santos <fabiosantosart@gmail.com> - 2013-05-24 09:47 +0100
Re: Simple algorithm question - how to reorder a sequence economically Chris Angelico <rosuav@gmail.com> - 2013-05-24 19:11 +1000
Re: Simple algorithm question - how to reorder a sequence economically duncan smith <buzzard@invalid.invalid> - 2013-05-24 15:33 +0100
Re: Simple algorithm question - how to reorder a sequence economically Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-24 06:10 -0400
Re: Simple algorithm question - how to reorder a sequence economically Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-24 10:52 +0000
Re: Simple algorithm question - how to reorder a sequence economically Ned Batchelder <ned@nedbatchelder.com> - 2013-05-24 07:26 -0400
Re: Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 06:23 -0700
Re: Simple algorithm question - how to reorder a sequence economically Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-24 13:57 +0000
Re: Simple algorithm question - how to reorder a sequence economically Robert Kern <robert.kern@gmail.com> - 2013-06-12 14:14 +0100
Re: Simple algorithm question - how to reorder a sequence economically John Ladasky <john_ladasky@sbcglobal.net> - 2013-05-24 10:33 -0700
Re: Simple algorithm question - how to reorder a sequence economically John Ladasky <john_ladasky@sbcglobal.net> - 2013-05-25 18:25 -0700
Re: Simple algorithm question - how to reorder a sequence economically Roy Smith <roy@panix.com> - 2013-05-25 21:49 -0400
RE: Simple algorithm question - how to reorder a sequence economically Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-24 18:00 +0300
Re: Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 12:01 -0700
RE: Simple algorithm question - how to reorder a sequence economically Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-25 00:33 +0300
Re: Simple algorithm question - how to reorder a sequence economically Peter Brooks <peter.h.m.brooks@gmail.com> - 2013-05-24 17:28 -0700
csiph-web