English

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

## Summary

In some instances, when there are too many point features, there may be interest in keeping only some of the points for statistical analysis. By selecting distributed points randomly, an unbiased analysis can be undertaken. The procedures provided below only select random points and do not create new random points. After selection, the 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. Refer to ArcMap: Subset Features for more information on the Subset Features tool.

Use Python scripts

```Note:
The following Python scripts are contributions from Stack Exchange and the workflow has been verified by Esri.```
1. In ArcMap, click the 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 percentage, 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
randOids = random.sample (oids, count)
oidsStr = ", ".join (map (str, randOids))
sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
arcpy.SelectLayerByAttribute_management (layer, "", sql)

SelectRandomByPercent ("<layer_name>", <percentage>)
```

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
randOids = random.sample (oids, count)
oidsStr = ", ".join (map (str, randOids))
sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
arcpy.SelectLayerByAttribute_management (layer, "", sql)

SelectRandomByPercent ("<layer_name>", <count_number>)
```

The following image is a sample of the script for percentage.

1. 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()```
1. Type rand() in the Random text box.
1. Click OK. Random numbers are generated in the new field.
1. Click the Table Options button and click Select By Attributes.
1. Type the following expression in the text box where <field_name> is the name of the new field, and num is the number of percent. Assuming the user wants to keep 50% of the features, the num value is 0.5.
`<field_name> < num`
1. Click Apply.

## Related Information

Last Published: 10/29/2020

Article ID: 000013141

Software: ArcMap 10.8.1, 10.8, 10.7.1, 10.7, 10.6.1, 10.6, 10.5.1, 10.5, 10.4.1, 10.4, 10.3.1, 10.3, 10.2.2, 10.2.1, 10.2, 10.1, 10