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. */
5 int *cs_randperm (int n, int seed)
6 {
7  int *p, k, j, t ;
8  if (seed == 0) return (NULL) ; /* return p = NULL (identity) */
9  p = (int*)cs_malloc (n, sizeof (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 int 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 }
void * cs_malloc(int n, size_t size)
Definition: cs_malloc.c:10
int * cs_randperm(int n, int seed)
Definition: cs_randperm.c:5


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:31