EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
cs_randperm.c
Go to the documentation of this file.
1 #include "cs.h"
2 /* return a random permutation vector, the identity perm, or p = n-1:-1:0.
3  * seed = -1 means p = n-1:-1:0. seed = 0 means p = identity. otherwise
4  * p = random permutation. */
6 {
7  CS_INT *p, k, j, t ;
8  if (seed == 0) return (NULL) ; /* return p = NULL (identity) */
9  p = cs_malloc (n, sizeof (CS_INT)) ; /* allocate result */
10  if (!p) return (NULL) ; /* out of memory */
11  for (k = 0 ; k < n ; k++) p [k] = n-k-1 ;
12  if (seed == -1) return (p) ; /* return reverse permutation */
13  srand (seed) ; /* get new random number seed */
14  for (k = 0 ; k < n ; k++)
15  {
16  j = k + (rand ( ) % (n-k)) ; /* j = rand integer in range k to n-1 */
17  t = p [j] ; /* swap p[k] and p[j] */
18  p [j] = p [k] ;
19  p [k] = t ;
20  }
21  return (p) ;
22 }
CS_INT * cs_randperm(CS_INT n, CS_INT seed)
Definition: cs_randperm.c:5
#define CS_INT
Definition: cs.h:627
void * cs_malloc(CS_INT n, size_t size)
Definition: cs_malloc.c:10