tag:www.mathworks.com,2005:/matlabcentral/fileexchange/feed?q=derangement MATLAB Central File Exchange icon.png logo.png MATLAB Central - File Exchange - derangement User-contributed code library 2021-07-26T17:14:28-04:00 6 1 60 87994 2021-03-02T22:09:52Z 2021-03-02T22:09:52Z Random derangement Generate sequences of random derangement

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.

Bruno Luong https://www.mathworks.com/matlabcentral/profile/390839-bruno-luong
30189 2019-05-23T12:19:36Z 2019-05-23T12:19:36Z RANDPERMFULL RANDPERMFULL (N) returns a random derangement (complete permutation) of the integers from 1 to N

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

Jos (10584) https://www.mathworks.com/matlabcentral/profile/870065-jos-10584
35324 2014-01-27T21:29:16Z 2014-01-27T21:29:16Z Generate uniform pseudo-random integers from linear range. RAND_INT(R,N) returns an n-by-n matrix containing pseudo-random integer values from range R.

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

DS https://www.mathworks.com/matlabcentral/profile/544298-ds
30101 2011-01-26T19:37:57Z 2011-01-26T19:37:57Z RPG Lab A set of functions for generating and testing random permutations of the integers (1,2, ..., n).

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.

Derek O'Connor https://www.mathworks.com/matlabcentral/profile/87230-derek-o-connor
27076 2011-03-06T19:17:02Z 2011-03-06T19:17:02Z Shuffle Random permutation of array elements, C-Mex: much faster than RANDPERM

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

Jan https://www.mathworks.com/matlabcentral/profile/869888-jan
5631 2004-09-03T09:42:18Z 2004-09-03T09:42:18Z PARTPERM -- Partial random permutation of a vector Partial random permutation of a vector.

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) &lt; 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).

David Fass https://www.mathworks.com/matlabcentral/profile/869847-david-fass