randp(m)
function output = randp(m)
%RANDP Random permutation
%   RANDP(n) is a random permutation of the integers from 1 to n.
%   For example, RANDP(6) might be [2 4 5 6 1 3].
%
%   Note that RANDPERM calls RAND and therefore changes RAND's state.
% Inspired by Statovic's Generate sorted vectors of uniformly distributed
% variates.
% Indexing sorted values, by using RANDPERM, is at best O(nlogn).
% Basing this process, instead, on Sattolo's version of Fisher-Yates
% shuffle algorithm for large vectors the result is at worst O(n).
%
%   See also RANDPERM, PERMUTE.

% Author: Ragaar
% Created: Oct 2009
% Copyright 2009
    output=1:m; % Initialize
    if m < 4
        % Computation time is insignificant for
        % small vectors and provides better randomization.
        [buffer,output]=sort(rand(1,m)); %#ok<ASGLU,NASGU>
    else
        for i=m:-1:2
            ii = ceil((i-1).*rand);
            buffer = output(ii);
            output(ii) = output(i);
            output(i) = buffer;
        end
    end
end

Contact us at files@mathworks.com