<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help altering some python code to randomly select one feature in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43280#M3419</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Did some quick tests selecting 25 random records against a ~700,000 record non-versioned feature class in Oracle.&amp;nbsp; Of the three different approaches I mentioned above (random.sample, reservoir sampling, and SQL sample), random.sample was the quickest taking about 4.5 seconds on average to make a feature layer.&amp;nbsp; SQL sample took about 1.4x longer than random.sample while reservoir sampling took 5.1x longer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems the overhead of calling random over N items becomes quite impactful with hundreds of thousands of records, and that impact will only grow as N grows.&amp;nbsp; Also, the memory impact from fully populating an OID list was quite a bit smaller than I anticipated.&amp;nbsp; The reservoir sampling code above is quite simple and isn't distributed, which I have seen some high-performing distributed reservoir sampling code, but it still demonstrates the trade-offs of different approaches.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The SQL sampling performed fairly well, a bit better than I expected.&amp;nbsp; Given it wasn't that much slower than random.sampling, it seems a more efficient piece of SQL might make the approach more competitive.&amp;nbsp; It would be interesting to see an SQL Server comparison, but that will have to wait for another day.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 16 Feb 2015 06:18:17 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2015-02-16T06:18:17Z</dc:date>
    <item>
      <title>Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43274#M3413</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Someone wrote the code below which works wonderfully to select a given percentage of polygons.&amp;nbsp; I have about 1 hour of experience with python, but would like to alter the code so that it selects a number of polygons (opposed to a percentage).&amp;nbsp; In particular, i would like it to allow me to select just one polygon at random.&amp;nbsp; Thanks in advance for your help.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Given an input layer, return an output *.lyr layer file containing a random&lt;/P&gt;&lt;P&gt;# selection of features.&lt;/P&gt;&lt;P&gt;#&lt;/P&gt;&lt;P&gt;# Original author: Leah Saunders, ESRI Inc, on Arcscripts.com. July 2007&lt;/P&gt;&lt;P&gt;# Major modification by Stephen Lead, 21st Feb 2008&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import os, sys, random, arcgisscripting&lt;/P&gt;&lt;P&gt;try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp = arcgisscripting.create()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.overwriteoutput = 1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Specify input featureclass, output *.lyr file and the percentage of&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # random points to return. Set these parameters in ArcToolbox as shown.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inputFC = sys.argv[1] # Feature Class or Feature Layer&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outputLyr = sys.argv[2] # Layer File&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inpct = sys.argv[3] # Long&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Ensure that the input percentage is between 1 and 100%&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inpct = min(int(inpct),100)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inpct = max(int(inpct),1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Work out how many features to select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inputDirname = os.path.dirname(inputFC)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inputBasename = os.path.basename(inputFC)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.workspace = inputDirname&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(inputFC)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; totpnts = gp.getcount(inputFC)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; numValues = int(round(totpnts * float(inpct) / 100.0))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("Selecting " + str(numValues) + " random features")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Generate a list of all features, and select randomly from this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inList = []&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; randomList = []&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldname = desc.OIDFieldName&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = gp.SearchCursor(inputFC)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage ("Loading all IDs into a list")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while row:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id = row.GetValue(fldname)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inList.append(id)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; selpnts = 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("Creating the list of randomly selected features")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while len(randomList) &amp;lt; numValues:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selpnts += 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selItem = random.choice(inList)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; randomList.append(selItem)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inList.remove(selItem)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select features whose OID value occurs in the random list, generate&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # a *.lyr file from this selection. (Leading and trailing [ and ] marks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # need to be removed from the list object)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; theLen = len(str(randomList))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sqlexp = '"' + fldname + '"' + " in " + "(" + str(randomList)[1:theLen - 1] + ")"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; selectionLyr = inputBasename + " selection"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer_management(inputFC, selectionLyr, sqlexp)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SaveToLayerFile_management(selectionLyr, outputLyr)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("\nOutput layer " + outputLyr + " contains features randomly selected from " + inputBasename + &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"\n")&lt;/P&gt;&lt;P&gt;except:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.adderror("Error running script. Try specifying the full path to the input layer")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# END OF FILE&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Feb 2015 20:12:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43274#M3413</guid>
      <dc:creator>JasonKelly1</dc:creator>
      <dc:date>2015-02-13T20:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43275#M3414</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am a VB programmer,not a Python guru. The changes I made are untested....&amp;nbsp; but doing something along the lines of the changes below should accomplish what you want .... without having to completely re-write the code:&amp;nbsp; (numPoly) is the number of randomly selected Polygons you wish between 1 and something less than the total # of possible polygons there are to select from.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Given an input layer, return an output *.lyr layer file containing a random
# selection of features.
#
# Original author: Leah Saunders, ESRI Inc, on Arcscripts.com. July 2007
# Major modification by Stephen Lead, 21st Feb 2008


import os, sys, random, arcgisscripting
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp = arcgisscripting.create()
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.overwriteoutput = 1


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Specify input featureclass, output *.lyr file and the percentage of
&amp;nbsp;&amp;nbsp;&amp;nbsp; # random points to return. Set these parameters in ArcToolbox as shown.
&amp;nbsp;&amp;nbsp;&amp;nbsp; inputFC = sys.argv[1] # Feature Class or Feature Layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; outputLyr = sys.argv[2] # Layer File
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #e23d39;"&gt;#inpct = sys.argv[3] # Long&lt;/SPAN&gt;
&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; numPoly = sys.argv[3] #Long Must be less than Actual Number of Polygons&lt;/SPAN&gt;


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Ensure that the input percentage is between 1 and 100%
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #e23d39;"&gt;#inpct = min(int(inpct),100)&lt;/SPAN&gt;
&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #inpct = max(int(inpct),1)&lt;/SPAN&gt;


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Work out how many features to select
&amp;nbsp;&amp;nbsp;&amp;nbsp; inputDirname = os.path.dirname(inputFC)
&amp;nbsp;&amp;nbsp;&amp;nbsp; inputBasename = os.path.basename(inputFC)


&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.workspace = inputDirname
&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(inputFC)
&amp;nbsp;&amp;nbsp;&amp;nbsp; totpnts = gp.getcount(inputFC)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #e23d39;"&gt;#numValues = int(round(totpnts * float(inpct) / 100.0))&lt;/SPAN&gt;
&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; numValues = numPoly&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("Selecting " + str(numValues) + " random features")


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Generate a list of all features, and select randomly from this
&amp;nbsp;&amp;nbsp;&amp;nbsp; inList = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; randomList = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; fldname = desc.OIDFieldName
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = gp.SearchCursor(inputFC)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage ("Loading all IDs into a list")
&amp;nbsp;&amp;nbsp;&amp;nbsp; while row:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id = row.GetValue(fldname)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inList.append(id)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()


&amp;nbsp;&amp;nbsp;&amp;nbsp; selpnts = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("Creating the list of randomly selected features")
&amp;nbsp;&amp;nbsp;&amp;nbsp; while len(randomList) &amp;lt; numValues:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selpnts += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selItem = random.choice(inList)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; randomList.append(selItem)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inList.remove(selItem)


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select features whose OID value occurs in the random list, generate
&amp;nbsp;&amp;nbsp;&amp;nbsp; # a *.lyr file from this selection. (Leading and trailing [ and ] marks
&amp;nbsp;&amp;nbsp;&amp;nbsp; # need to be removed from the list object)
&amp;nbsp;&amp;nbsp;&amp;nbsp; theLen = len(str(randomList))
&amp;nbsp;&amp;nbsp;&amp;nbsp; sqlexp = '"' + fldname + '"' + " in " + "(" + str(randomList)[1:theLen - 1] + ")"
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectionLyr = inputBasename + " selection"
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer_management(inputFC, selectionLyr, sqlexp)
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SaveToLayerFile_management(selectionLyr, outputLyr)


&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("\nOutput layer " + outputLyr + " contains features randomly selected from " + inputBasename + "\n")
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.adderror("Error running script. Try specifying the full path to the input layer")


# END OF FILE&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:39:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43275#M3414</guid>
      <dc:creator>TedKowal</dc:creator>
      <dc:date>2021-12-10T21:39:39Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43276#M3415</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;or if you have a Geostatistical Analyst license you could use the &lt;A href="http://desktop.arcgis.com/en/desktop/latest/tools/geostatistical-analyst-toolbox/subset-features.htm"&gt;SubsetFeatures &lt;/A&gt;gp tool, viz.,&lt;/P&gt;&lt;P&gt;arcpy.SubsetFeatures_ga('myPolys', 'outPolys', '', 5, "ABSOLUTE_VALUE") will return 5 randomly selected polygons. and&lt;/P&gt;&lt;P&gt;arcpy.SubsetFeatures_ga('myPolys', 'outPolys', '', 5, "PERCENTAGE_OF_INPUT") will return a random 5% of the polygons.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or if you have n polygons, then generate 5 random numbers between 1 and n and use these as polygon id's and extract those features. Which is basically what the script above does.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-Steve&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Feb 2015 21:49:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43276#M3415</guid>
      <dc:creator>SteveLynch</dc:creator>
      <dc:date>2015-02-13T21:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43277#M3416</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could use this code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy
&amp;nbsp;&amp;nbsp;&amp;nbsp; from random import randint

&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_in = arcpy.GetParameterAsText(0) # input featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp; fl_out = arcpy.GetParameterAsText(1) # output layerfile
&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = arcpy.GetParameter(2) # number of features to select

&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt_feats = int(arcpy.GetCount_management(fc_in).getOutput(0))

&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt &amp;gt; 0 and cnt &amp;lt; cnt_feats:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_oid = arcpy.Describe(fc_in).OIDFieldname
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lst_oids = [r[0] for r in arcpy.da.SearchCursor(fc_in, (fld_oid))]

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sel_oid = []
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while len(sel_oid) &amp;lt; cnt:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = randint(0, cnt_feats-1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oid = lst_oids&lt;I&gt;&lt;/I&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not str(oid) in sel_oid:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sel_oid.append(str(oid))

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oids = ", ".join(sel_oid)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(fc_in, fld_oid), oids)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc_in, "selection", where)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management("selection", fl_out)

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:39:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43277#M3416</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-10T21:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43278#M3417</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If we are already importing random, then we can rely on &lt;SPAN style="font-family: courier new,courier;"&gt;random.sample&lt;/SPAN&gt; to do the heavy lifting for us, assuming we have already went to the effort of building an OID list and want sampling without replacement.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def main():&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; from random import sample&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_in = arcpy.GetParameterAsText(0) # input featureclass&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; fl_out = arcpy.GetParameterAsText(1) # output layerfile&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = arcpy.GetParameter(2) # number of features to select

&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_oid = arcpy.Describe(fc_in).OIDFieldname
&amp;nbsp;&amp;nbsp;&amp;nbsp; lst_oids = [oid for oid, in arcpy.da.SearchCursor(fc_in, (fld_oid))]

&amp;nbsp;&amp;nbsp;&amp;nbsp; oids = ", ".join(map(str, sample(lst_oids, cnt)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(fc_in, fld_oid), oids)

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc_in, "selection", where)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management("selection", fl_out)&amp;nbsp; 

if __name__ == '__main__':&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If building an OID list in-memory becomes an issue, one could resort to using a reservoir sampling approach.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def stream_sample(iterator, k):
&amp;nbsp;&amp;nbsp;&amp;nbsp; from random import randint
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = [next(iterator) for _ in range(k)]

&amp;nbsp;&amp;nbsp;&amp;nbsp; n = k
&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in iterator:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = randint(0, n)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if s &amp;lt; k:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result&lt;S&gt; = item&lt;/S&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return result
 
 def main():&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy&amp;nbsp; 
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_in = arcpy.GetParameterAsText(0) # input featureclass&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; fl_out = arcpy.GetParameterAsText(1) # output layerfile&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = arcpy.GetParameter(2) # number of features to select
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_oid = arcpy.Describe(fc_in).OIDFieldname
&amp;nbsp;&amp;nbsp;&amp;nbsp; sample_oids = [oid for oid, in stream_sample(arcpy.da.SearchCursor(fc_in, "OID@"), cnt)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; oids = ", ".join(map(str, sample_oids))
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(fc_in, fld_oid), oids)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc_in, "selection", where)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management("selection", fl_out)&amp;nbsp; 

if __name__ == '__main__':&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; main() &lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A plus of using reservoir sampling is that the memory footprint can be quite modest to trivial when working with very large datasets.&amp;nbsp; A minus of using reservoir sampling is that calling random so many times can add noticeable overhead; that said, I can still sample from a million records in a couple seconds.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The stream_sample function is taken from JesseBuesking on the StackExchange thread: &lt;A href="http://stackoverflow.com/questions/9690009/pick-n-items-at-random" rel="nofollow noopener noreferrer" target="_blank"&gt;pick N items at random&lt;/A&gt;.&amp;nbsp; The code from JesseBuesking is basically just implementing Don Knuth's algorithm for picking random elements from a set whose cardinality is unknown. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:39:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43278#M3417</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-10T21:39:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43279#M3418</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A completely different approach to relying on application logic to select random records would be to rely on the database management system to select random records.&amp;nbsp; If someone is using an enterprise DBMS (Oracle, SQL Server, PostgreSQL, etc..), the database will support some way of returning randomly selected records.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def main():&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; import os

&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_in = arcpy.GetParameterAsText(0) # input featureclass&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; fl_out = arcpy.GetParameterAsText(1) # output layerfile&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = arcpy.GetParameter(2) # number of features to select
&amp;nbsp;&amp;nbsp;&amp;nbsp; dbms = # use 'oracle' or 'sqlserver'

&amp;nbsp;&amp;nbsp;&amp;nbsp; oid_fld = arcpy.Describe(fc_in).OIDFieldName

&amp;nbsp;&amp;nbsp;&amp;nbsp; oracle_where_clause = (
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "{} IN (SELECT {} FROM "
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "(SELECT {} FROM {} ORDER BY dbms_random.value) "
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "WHERE rownum &amp;lt;= {})".format(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oid_fld, oid_fld, oid_fld, os.path.split(fc_in)[1], cnt
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; )
&amp;nbsp;&amp;nbsp;&amp;nbsp; )
&amp;nbsp;&amp;nbsp; sqlserver_where_clause = (
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "{} IN (SELECT TOP {} {} FROM {} ORDER BY NEWID())".format(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oid_fld, cnt, oid_fld, os.path.split(fc_in)[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; )
&amp;nbsp;&amp;nbsp;&amp;nbsp; ) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if dbms == 'oracle':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause = oracle_where_clause
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif dbms == 'sqlserver':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause = sqlserver_where_clause

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc_in, "tmpLayer")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("tmpLayer", "NEW_SELECTION", where_clause)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("tmpLayer", "selection")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management("selection", fl_out)


if __name__ == '__main__':&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; main() &lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In many ways, this would &lt;SPAN style="text-decoration: underline;"&gt;not&lt;/SPAN&gt; be a very good "general" approach for several reasons.&amp;nbsp; One, relying on the DBMS makes the code more involved or less portable because every DBMS seems to have a different approach to selecting random records.&amp;nbsp; Second, passing SQL through ArcGIS tools always seems to have a sketchiness about it.&amp;nbsp; It does work, but I have definitely run into issues as well.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looking at the code above, one might wonder why lines 29 and 30 exist, i.e., why not just pass the SQL directly to the MakeFeatureLayer tool.&amp;nbsp; When I first ginned up this code, I tried doing just that, but I found an interesting/odd behavior.&amp;nbsp; The SQL to select random records actually became embedded in the definition of the feature layer so every time ArcMap was refreshed, the records kept changing.&amp;nbsp; ArcMap didn't like that, there would be issues with displaying polygons at times.&amp;nbsp; It would be neat, though, with&amp;nbsp; attribute-only data to load a dynamically, randomly changing table into ArcMap for testing at times.&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:39:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43279#M3418</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-10T21:39:48Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43280#M3419</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Did some quick tests selecting 25 random records against a ~700,000 record non-versioned feature class in Oracle.&amp;nbsp; Of the three different approaches I mentioned above (random.sample, reservoir sampling, and SQL sample), random.sample was the quickest taking about 4.5 seconds on average to make a feature layer.&amp;nbsp; SQL sample took about 1.4x longer than random.sample while reservoir sampling took 5.1x longer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems the overhead of calling random over N items becomes quite impactful with hundreds of thousands of records, and that impact will only grow as N grows.&amp;nbsp; Also, the memory impact from fully populating an OID list was quite a bit smaller than I anticipated.&amp;nbsp; The reservoir sampling code above is quite simple and isn't distributed, which I have seen some high-performing distributed reservoir sampling code, but it still demonstrates the trade-offs of different approaches.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The SQL sampling performed fairly well, a bit better than I expected.&amp;nbsp; Given it wasn't that much slower than random.sampling, it seems a more efficient piece of SQL might make the approach more competitive.&amp;nbsp; It would be interesting to see an SQL Server comparison, but that will have to wait for another day.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Feb 2015 06:18:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43280#M3419</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-02-16T06:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43281#M3420</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wow, thanks everyone for your responses!&amp;nbsp; So far I've only had a chance to try Ted's code.&amp;nbsp; Ted, for some reason, it didn't work, but after seeing what you did, which made a lot of sense to me (my background is also in VB), I simply replaced this line from the original code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numValues = int(round(totpnts * float(inpct) / 100.0))&amp;nbsp; &lt;/P&gt;&lt;P&gt;with this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numValues = int(inpct) &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Which seems to work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks everyone else for your scripts, too; going through each of them, figuring out what you've done, and then tinkering with them will be very helpful as I try to familiarize myself with Python.&amp;nbsp; Much appreciated!&lt;/P&gt;&lt;P&gt;-Jason&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Feb 2015 14:03:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43281#M3420</guid>
      <dc:creator>JasonKelly1</dc:creator>
      <dc:date>2015-02-16T14:03:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help altering some python code to randomly select one feature</title>
      <link>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43282#M3421</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just gave an approach based upon your code to get you unstuck.... I figured the experts would provide the "best practice" alternatives, which greatly helps coding development providing you understand the solution.&amp;nbsp; I did enjoy the finesse displayed at the numerous ways the problem was attacked. You hit upon my gripe with python -- does not work consistently between machines. &lt;/P&gt;&lt;P&gt;The code I sent works on mine.&amp;nbsp; I have used other code that works for another person but does not work in my environment.&amp;nbsp; At least with VB it was getting the correct reference then the code worked on any machine.&amp;nbsp; I do not have a lot of confidence stating the code will work for just because it works on my machine.&amp;nbsp; So I enjoy lots of different ways of attacking the problem (give greater chance that one solution will work for me!)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Feb 2015 15:44:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-altering-some-python-code-to-randomly-select/m-p/43282#M3421</guid>
      <dc:creator>TedKowal</dc:creator>
      <dc:date>2015-02-17T15:44:50Z</dc:date>
    </item>
  </channel>
</rss>

