A derangement is a permutation of the elements of a set, such that no element appears in its original position.The code use non rejection method for better predictable runtime.

RANDPERMFULL(N) is a random complete or full permutation of the integers from 1 to N; each integer is randomly assigned to an index position that is different from its value. In other words R = RANDPERMFULL(N) creates a row vector R, such that R(j) ~= j for all j=1:N. This type of permutation is called a derangement.N should be larger than 1.Example:randpermfull(7) % might be [2 4 5 6 1 7 3], but not [2 1 3 6 4 7 5]% since 3 cannot be in index position 3. % Scramble a sentenceWords = {'can', 'you', 'make', 'sense', 'of', 'this', 'scrambled', 'sentence'} ;R = randpermfull(numel(Words)) ;disp([sprintf('%s ',Words{R}) '?']) Reference: http://en.wikipedia.org/wiki/Derangement

Function that uses RAND to generate random integers in the specified linear range, as follows:result = floor(a + (b-a+1).* rand(N)),where specified range is [a b]-----Please note: There are lots of ways to do this and lots of interesting submissions on FEX which provide similar results (see below).random numbers:#18443 - Random Integer Generator#5346 - MYRANDINT -- Random Integer Generation#27942 - WWW.RANDOM.ORG random integers/sequences/strings#27809 - Unique Random Integer List#13007 - Random Number Generator#21353 - True Random Integer Generator (random.org)random arrangement:#30189 - RANDPERMFULL (derangement)#17981 - RANDBLOCK#27076 - Shuffle

The main functions provided are:1. p = GRPfys(n); generates a random permutation of length n.2. p = GRPmex(n); generates a random permutation of length n.3. p = GRDrej(n); generates a derangement of length n.4. p = GRDmex(n); generates a derangement of length n.5. p = GRDMPP(p); generates a derangement of length n.6. p = GRPcyc(p); generates a cyclic permutation of length n.7. Various interrogation functions, e.g., IsCyc(p).Included is a set of notes on Random Permutations and their implementation and testing.

Shuffle - Random permutation of array elementsThis function is equivalent to X(RANDPERM(LENGTH(X)), but 50% to 85% faster. It uses D.E. Knuth's shuffle algorithm (also called Fisher-Yates) and the cute KISS random number generator (G. Marsaglia). While RANDPERM needs 2*LENGTH(X)*8 bytes as temporary memory, SHUFFLE needs just a fixed small number of bytes.1. Inplace shuffling: Y = Shuffle(X, Dim)INPUT: X: DOUBLE, SINGLE, CHAR, LOGICAL, (U)INT64/32/16/8 array. Dim: Dimension to operate on. Optional, default: 1st non-singleton dimension.OUTPUT: Y: Array of same type and size as X with shuffled elements.2. Create a shuffle index: Index = Shuffle(N, 'index', NOut)This is equivalent to Matlab's RANDPERM, but much faster, if N is large and NOut is small.INPUT: N: Integer number. NOut: The number of output elements. Optional, default: N.OUTPUT: Index: [1:NOut] elements of shuffled [1:N] vector in the smallest possible integer type.3. Derangement index:Index = Shuffle(N, 'derange', NOut)Equivalent to the index method, but all Index[i] ~= i. A rejection method is used: Create an index vector until a derangement is gained.EXAMPLES: R = Shuffle(1:8) % [8, 1, 2, 6, 4, 3, 5, 7] R = Shuffle('abcdefg') % 'efbadcg' R = Shuffle([1:4; 5:8], 2) % [3, 2, 1, 4; 6, 8, 7, 5] I = Shuffle(8, 'index'); % UINT8([1, 5, 7, 6, 2, 3, 4, 8])Choose 10 different rows from a 1000 x 100 matrix: X = rand(1000, 100); Y = X(Shuffle(1000, 'index', 10), :);Operate on cells or complex arrays: C = {9, 's', 1:5}; SC = C(Shuffle(numel(C), 'index')); M = rand(3) + i * rand(3); SM = M(:, Shuffle(size(C, 2), 'index'))NOTES: There are several other shuffle functions in the FEX. Some use Knuth's method also, some call RANDPERM. This implementation is faster due to calling a compiled MEX file and it has a smaller memory footprint. The KISS random numbers are much better than the RAND() of the C-standard libs.Run the unit-test TestShuffle to test validity and speed (see screenshot).Tested: Matlab 6.5, 7.7, 7.8, 32bit, WinXP,Compiler: LCC 2.4/3.8, BCC 5.5, Open Watcom 1.8, MSVC 2008.Compatibility to 64 bit, Linux and Mac is assumed.Pre-compiled Mex: http://www.n-simon.de/mex

X = PARTPERM(A,IDX) randomly permutes the elements of vector A which are specified by vector IDX. That is, the elements A(IDX) are randomly interchanged with one another and the resulting (partially permuted) vector is returned. No permutation occurs when length(IDX) < 2. By default, permutation is subject to the constraint that all elements specifed by IDX must appear in new positions ("derangement"). If you don't like this, use the following syntax: X = PARTPERM(A,IDX,'allow') randomly permutes the elements of vector A which are specified by vector IDX, but allows indexed elements to remain unchanged if that is the natural result of the random permutation (i.e., if after permutation an element's new location happens to be the same as its original location).