1 #include "cs_mex.h"
2 /* cs_randperm: random permutation. p=cs_randperm(n,0) is 1:n,
3 * p=cs_randperm(n,-1) is n:-1:1. p = cs_randperm (n,seed) is a random
4 * permutation using the given seed (where seed is not 0 or -1).
5 * seed defaults to 1. A single seed always gives a repeatable permutation.
6 * Use p = cs_randperm(n,rand) to get a permutation that varies with each use.
7 */
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])8 void mexFunction
9 (
10 int nargout,
11 mxArray *pargout [ ],
12 int nargin,
13 const mxArray *pargin [ ]
14 )
15 {
16 double seed ;
17 csi iseed, n, *p ;
18 if (nargout > 1 || nargin < 1 || nargin > 2)
19 {
20 mexErrMsgTxt ("Usage: p = cs_randperm(n,seed)") ;
21 }
22 seed = (nargin > 1) ? mxGetScalar (pargin [1]) : 1 ;
23 iseed = (seed > 0 && seed < 1) ? (seed * RAND_MAX) : seed ;
24 n = mxGetScalar (pargin [0]) ;
25 n = CS_MAX (n, 0) ;
26 p = cs_randperm (n, iseed) ;
27 pargout [0] = cs_mex_put_int (p, n, 1, 1) ; /* return p */
28 }
29