random selection o polygons

James Darrell McCauley mccauley at mcs.com
Wed Nov 29 07:00:00 EST 1995

Teresa Hansen (teresa at lewis-deh2.army.mil) writes on 29 November 1995:
>I have a raster file depicting 64 seperately labels polygons.
>How can I randomly select 34 of those polygons & output to a new file???

use a simple program to randomly select, without replacement,
34 out of 64 categories (I coded one up for you, attached). 
Then use r.reclass. You should be able to modify the attached
program to write a reclass rules file.
Darrell McCauley, PhD; mccauley at mcs.com; http://www.mcs.com/~mccauley/

 * short program to randomly select, without replacement, NSELECT objects
 * out of POOLSIZE
 * Written by Darrell McCauley <mccauley at mcs.com> 
 * Note: rand may not be available on all systems and is not a good RNG.
 * Do not use rand for designing missle intercept systems or for medical
 * research.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

#define RANDOM(lo,hi) ((float)rand()/RAND_MAX*(hi-lo)+lo)
#define POOLSIZE 64
#define NSELECT 34

int main ()
  int i, n = 0;
  char pool[POOLSIZE];

  srand (time (NULL));
  for (i = 0; i < POOLSIZE; i++)
    pool[i] = NULL;
  while (n < NSELECT)
    i = (int) RANDOM (1, POOLSIZE) - 1;
    if (pool[i] == NULL)
      fprintf (stderr, ".");
      pool[i] = 1;
  fprintf (stderr, "\n");
  for (i = 0; i < POOLSIZE; i++)
    if (pool[i] != NULL)
      printf ("%d\n", i);
  return 0;

More information about the grass-user mailing list