English

How To: Select random points from an existing point feature layer

Summary

In some instances, when there are too many point features, the user is interested in keeping only some of the points for statistical analysis. By selecting distributed points randomly, an unbiased analysis can be undertaken. The procedures provided only select random points and do not create new random points. However, after selection, the selected points can be exported to a new layer.

The instructions provided describe how to select random points from an existing point feature layer.

Procedure

The following describes three possible workflows using the Subset Features tool, Python, or the Select By Attributes function.

  • Use the Subset Features tool

    The Subset Features tool divides the data into two subsets. The features are divided by generating random values from a uniform [0,1] distribution. This tool requires the Geostatistical Analyst license. For more information on the Subset Features tool, refer to the following ArcGIS web help, Subset Features.
     
  • Use Python scripts
    1. In ArcMap, click the Python button python_button to open the Python window.
    2. Copy and paste one of the following Python scripts to the Python window.

      Use Script 1 to select random point features based on percent, or Script 2 to select random point features based on counts.

      Script 1
      import arcpy
      def SelectRandomByPercent (layer, percent):
          #layer variable is the layer name in TOC
          #percent is percent as whole number  (0-100)
          if percent > 100:
              print "percent is greater than 100"
              return
          if percent < 0:
              print "percent is less than zero"
              return
          import random
          fc = arcpy.Describe (layer).catalogPath
          featureCount = float (arcpy.GetCount_management (fc).getOutput (0))
          count = int (featureCount * float (percent) / float (100))
          if not count:
              arcpy.SelectLayerByAttribute_management (layer, "CLEAR_SELECTION")
              return
          oids = [oid for oid, in arcpy.da.SearchCursor (fc, "OID@")]
          oidFldName = arcpy.Describe (layer).OIDFieldName
          delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName)
          randOids = random.sample (oids, count)
          oidsStr = ", ".join (map (str, randOids))
          sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
          arcpy.SelectLayerByAttribute_management (layer, "", sql)

      Script 2
      import arcpy
      def SelectRandomByCount (layer, count):
          import random
          layerCount = int (arcpy.GetCount_management (layer).getOutput (0))
          if layerCount < count:
              print "input count is greater than layer count"
              return
          oids = [oid for oid, in arcpy.da.SearchCursor (layer, "OID@")]
          oidFldName = arcpy.Describe (layer).OIDFieldName
          delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName)
          randOids = random.sample (oids, count)
          oidsStr = ", ".join (map (str, randOids))
          sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
          arcpy.SelectLayerByAttribute_management (layer, "", sql)
    3. In the Python window, press Enter to execute the script.
    4. Type the following code, where the parameter layer is the name of the layer, and num is the number of percent or count.
      SelectRandomByPercent ("layer", num)
    5. Press Enter to run the script.
  • Use Select By Attributes
    1. Generate random values in a new field.
      1. Create a new Float field.
      2. Right-click the new field header and click Field Calculator.
      3. Change the parser to Python.
      4. Check the Show Codeblock check box.
      5. Copy the following code, and paste to the Pre-Logic Script Code text box.
        import random
        def rand():
           return random.random()
      6. Type rand() in the Random text box.
      7. Click OK. Random numbers are generated in the new field.
    2. Click the Table Options button and click Select By Attributes.
    3. Type the following expression in the text box where layer is the name of the layer, and num is the number of percent. Assuming the user wants to keep 50% of the features, the num value is 0.5.
      "layer"<num
    4. Click Apply.

Related Information