Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Randperm, Randi, and Shuffle
Date: Thu, 9 Dec 2010 14:56:06 +0000 (UTC)
Organization: Erasmus MC
Lines: 37
Message-ID: <idqqm6$rdv$1@fred.mathworks.com>
References: <iaq1os$sak$1@fred.mathworks.com> <idpcig$7a8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1291906566 28095 172.30.248.35 (9 Dec 2010 14:56:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 9 Dec 2010 14:56:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:693889

"Derek O'Connor" <derekroconnor@eircom.net> wrote in message <idpcig$7a8$1@fred.mathworks.com>...
> This is slightly off-topic but may be of interest:
> 
> function p = GenPerm(n,m);
> %
> % Generate a random cyclic permutation using Sattolo's
> % modification of Knuth's Algorithm P, Section 3.4.2
> % TAOCP, Vol 2, 2nd Ed.
> %
> % m = 0 is the Fisher-Yates-Knuth Shuffle Algorithm P
> % m = 1 is Sattolo's Algorithm
> %
> % Prodhinger :  On the analysis of an algorithm to generate
> %                     a random cyclic permutation.
> % http://math.sun.ac.za/~hproding/abstract/abs_161.htm
> %
> % Wilson : Random and exhaustive generation of permutations
> %              and cycles
> % http://arxiv.org/abs/math/0702753
> % Note: Wilson uses zero-based arrays.
> %
> % Derek O'Connor, 8 Dec 2010.  derekroconnor@eircom.net
> 
> p = 1:n;  % Identity permutation
> 
> for k = n:-1:2
>    r = 1 + floor(rand*(k-m)); % random integer between 1 and k-m
>    t     =  p(r);
>    p(r) = p(k);                      % Swap(p(r),p(k))
>    p(k) = t;
> end;
> return; % GenCycPerm

You might also like to take a look at (the code) of RANDPERMFULL
http://www.mathworks.com/matlabcentral/fileexchange/23257-randpermfull

Jos