cs_randperm.c
Go to the documentation of this file.
00001 #include "cs.h"
00002 /* return a random permutation vector, the identity perm, or p = n-1:-1:0.
00003  * seed = -1 means p = n-1:-1:0.  seed = 0 means p = identity.  otherwise
00004  * p = random permutation.  */
00005 int *cs_randperm (int n, int seed)
00006 {
00007     int *p, k, j, t ;
00008     if (seed == 0) return (NULL) ;      /* return p = NULL (identity) */
00009     p = cs_malloc (n, sizeof (int)) ;   /* allocate result */
00010     if (!p) return (NULL) ;             /* out of memory */
00011     for (k = 0 ; k < n ; k++) p [k] = n-k-1 ;
00012     if (seed == -1) return (p) ;        /* return reverse permutation */
00013     srand (seed) ;                      /* get new random number seed */
00014     for (k = 0 ; k < n ; k++)
00015     {
00016         j = k + (rand ( ) % (n-k)) ;    /* j = rand int in range k to n-1 */
00017         t = p [j] ;                     /* swap p[k] and p[j] */
00018         p [j] = p [k] ;
00019         p [k] = t ;
00020     }
00021     return (p) ;
00022 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


hogman_minimal
Author(s): Maintained by Juergen Sturm
autogenerated on Wed Dec 26 2012 15:36:47