Re: quick algorithm for random permutation
- From: John <john@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 2 Feb 2007 20:50:28 +0000 (UTC)
Ben Pfaff <blp@xxxxxxxxxxxxxxx> wrote:
John <john@xxxxxxxxxxxxxxxxxxxxxxxxxx> writes:
I need a "quick" algorithm to generate a random permutation
of 1...n. Quick means that for n=32000 or thereabouts,
it should be able to run a few hundred times on a p4-class
machine in a few seconds (i.e., before users start fidgeting).
Here's an efficient shuffle implementation in C:
http://benpfaff.org/writings/clc/shuffle.html
Thanks, again, Ben, and everyone else who replied.
This algorithm works great, was easy as pie to implement,
and runs as fast as a bat out of heck. So my problem's
solved.
But one additional "aesthetic" issue presented itself.
As is typical, I'm using the generated permutation of 1...n
to permute another array of objects. But this algorithm
can permute the objects directly, without first explicitly
generating the 1...n permutation and then applying it to
the array. So that's what I'd obviously prefer to do.
However, later on, I have to apply the inverse permutation
(unscramble the egg, so to speak). At this later point
the random number generator is pumped to exactly the same
spot, so the original sequence of (pseudo) random numbers
is reproduced. And if I explicitly generate the 1...n
permutation, it's easy to just read off the inverse.
But, is there a way to directly "inversely permute" the
permuted array of objects. For example, note that
Mike Robson's post discusses the same algorithm, but
loops from n-1...0 instead of 0...n-1. This doesn't
seem to be the inverse permutation as far as I can tell,
but is there some such modification of the original
algorithm that is the inverse?
Again, this is mostly an intellectual curiosity/aesthetic
question, as the program's running just fine as is.
But I'd gladly spend a little more time to gild its lily
if that's possible. Thanks so much for all your previous
help, and for any further ideas about this,
--
John Forkosh ( mailto: j@xxxxx where j=john and f=forkosh )
.
- Follow-Ups:
- Re: quick algorithm for random permutation
- From: jafet . vixle
- Re: quick algorithm for random permutation
- References:
- quick algorithm for random permutation
- From: John
- Re: quick algorithm for random permutation
- From: Ben Pfaff
- quick algorithm for random permutation
- Prev by Date: Re: Exclusive-Or used to memorize an infinite list of numbers
- Next by Date: Re: Hofman and Diaby talk about P=NP at INFORMS 2007
- Previous by thread: Re: quick algorithm for random permutation
- Next by thread: Re: quick algorithm for random permutation
- Index(es):
Relevant Pages
|