Path: utzoo!attcan!uunet!husc6!cca!g-rh
From: g-rh@cca.CCA.COM (Richard Harter)
Newsgroups: comp.lang.misc
Subject: Re: Random permutation algorithm
Message-ID: <32265@cca.CCA.COM>
Date: 17 Aug 88 16:26:07 GMT
References: <5200012@m.cs.uiuc.edu>
Reply-To: g-rh@CCA.CCA.COM (Richard Harter)
Organization: Computer Corp. of America, Cambridge, MA
Lines: 38

In article <5200012@m.cs.uiuc.edu> robison@m.cs.uiuc.edu writes:

>Though the following appears to be a math problem, I'm posting in comp.lang.misc
>because the problem was stated in the context of imperative languages vs.
>applicative languages.  [1] says that the fastest imperative algorithm takes
>O(n) time, while the fastest applicative algorithm takes O(n log n) time.

>    Problem: Random Permutation

>    Description: Given n and a sequence of n integers drawn independently
>                 from a uniform distribution over 1..n, produce a uniformly
>                 random permutation of the sequence 1..n.

>My question is: Am I either misreading the question?  It would seem to
>be impossible.  There are n^n possible inputs, and n! possible outputs.  
>In general n! does not divide n^n evenly.  Can anyone clarify this, or 
>put me in contact with the authors?

	You are misreading the question.  There is only one input, namely
the given sequence of length n.  The problem is that you are given a
sequence of integers a1 ... an.  Generate a uniformly random permutation of
that sequence.  From the statement of the problem there may be duplicates
in the sequence, but that is irrelevant.  The imperative algorithm runs as 
follows:

	for i from [1...n] sequentially chosen
	  select j from [i...n] randomly
	  exchange a(i),a(j)
	  end loop


---

-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.