<?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 Create Table of Random Numbers in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31222#M2480</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a feature class table of OBJECTIDs that I would like to use to create a table of random numbers.&amp;nbsp; This output table of random numbers will be used to Relate or Join back to the original feature class in order to select those records to QA/QC.&amp;nbsp; I will be doing this repeatedly and would like this to be ArcGIS script tool.&amp;nbsp; I need to find the minimum, maximum, and range of the OBJECTIDs and then select a random sample of only 10% (or other % if desired) of that range from which to generate the random numbers.&amp;nbsp; I know the Python random.sample(range(min,max),range) will work, but I am having trouble with the coding part.&amp;nbsp; Any help is greatly appreciated.&amp;nbsp; My Python skills are currently C+.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#The goal of this script is to take a list of OBJECTID values from a feature class
# and find the Minimum, Maximum, and Range of that list. Using that information
#&amp;nbsp; then create a list of random numbers between the min and the max that is 
#&amp;nbsp;&amp;nbsp; equal to 10% of the range.&amp;nbsp; The output should be a table with a column
#&amp;nbsp;&amp;nbsp;&amp;nbsp; containing the random 10% values which can then be related or joined to the 
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; source feature class to identify a 10% random sample to analyze further.

#Import the arcpy and Random modules
import arcpy, os, random

#Overwrite if files already exists
arcpy.env.overwriteOutput = True

#Setting up the paramters for the ArcGIS Python Script Tool
inputfc = 'L:\\Mapping-GIS\\PLN\\GIS\\Layers\\Geodatabases\\Parcels_YearBuilt\\SWFWMD_2013_q4.gdb\\Pasco'
outputfc = "L:/Mapping-GIS/PLN/GIS/Projects/PSSA/PSSA_Edits.mdb/parcel_stats"

#Setting up the paramters for the OBJECTID field, or similar, to be used to generate the random numbers list from
statsField = "OBJECTID"

#Calculate Minimum, Maximum, Range and 10% of Range of the fc's OBJECTID field&amp;nbsp; 
#...Used to determine the numbers that will be used to calc a random list from which later
#....can be used to relate to the original OBJECTIDs to select those records which to scrutinuze further in the QA/QC process
arcpy.Statistics_analysis(inputfc, outputfc, [[statsField, "MIN"], [statsField, "MAX"], [statsField, "RANGE"]])
arcpy.AddField_management(outputfc, "PERCENT_10", "DOUBLE")
arcpy.CalculateField_management(outputfc, "PERCENT_10", "[RANGE_OBJECTID] * 0.10")

#Testing the functionality of the random number generator
# Just an example of the numbers, but I need these inputs here, to come as parameters from the outputs of the Statistics table above.
x = random.sample(range(100550,355150),25460)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 20 May 2014 13:56:53 GMT</pubDate>
    <dc:creator>Corey_C_Denninger</dc:creator>
    <dc:date>2014-05-20T13:56:53Z</dc:date>
    <item>
      <title>Create Table of Random Numbers</title>
      <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31222#M2480</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a feature class table of OBJECTIDs that I would like to use to create a table of random numbers.&amp;nbsp; This output table of random numbers will be used to Relate or Join back to the original feature class in order to select those records to QA/QC.&amp;nbsp; I will be doing this repeatedly and would like this to be ArcGIS script tool.&amp;nbsp; I need to find the minimum, maximum, and range of the OBJECTIDs and then select a random sample of only 10% (or other % if desired) of that range from which to generate the random numbers.&amp;nbsp; I know the Python random.sample(range(min,max),range) will work, but I am having trouble with the coding part.&amp;nbsp; Any help is greatly appreciated.&amp;nbsp; My Python skills are currently C+.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#The goal of this script is to take a list of OBJECTID values from a feature class
# and find the Minimum, Maximum, and Range of that list. Using that information
#&amp;nbsp; then create a list of random numbers between the min and the max that is 
#&amp;nbsp;&amp;nbsp; equal to 10% of the range.&amp;nbsp; The output should be a table with a column
#&amp;nbsp;&amp;nbsp;&amp;nbsp; containing the random 10% values which can then be related or joined to the 
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; source feature class to identify a 10% random sample to analyze further.

#Import the arcpy and Random modules
import arcpy, os, random

#Overwrite if files already exists
arcpy.env.overwriteOutput = True

#Setting up the paramters for the ArcGIS Python Script Tool
inputfc = 'L:\\Mapping-GIS\\PLN\\GIS\\Layers\\Geodatabases\\Parcels_YearBuilt\\SWFWMD_2013_q4.gdb\\Pasco'
outputfc = "L:/Mapping-GIS/PLN/GIS/Projects/PSSA/PSSA_Edits.mdb/parcel_stats"

#Setting up the paramters for the OBJECTID field, or similar, to be used to generate the random numbers list from
statsField = "OBJECTID"

#Calculate Minimum, Maximum, Range and 10% of Range of the fc's OBJECTID field&amp;nbsp; 
#...Used to determine the numbers that will be used to calc a random list from which later
#....can be used to relate to the original OBJECTIDs to select those records which to scrutinuze further in the QA/QC process
arcpy.Statistics_analysis(inputfc, outputfc, [[statsField, "MIN"], [statsField, "MAX"], [statsField, "RANGE"]])
arcpy.AddField_management(outputfc, "PERCENT_10", "DOUBLE")
arcpy.CalculateField_management(outputfc, "PERCENT_10", "[RANGE_OBJECTID] * 0.10")

#Testing the functionality of the random number generator
# Just an example of the numbers, but I need these inputs here, to come as parameters from the outputs of the Statistics table above.
x = random.sample(range(100550,355150),25460)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 May 2014 13:56:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31222#M2480</guid>
      <dc:creator>Corey_C_Denninger</dc:creator>
      <dc:date>2014-05-20T13:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table of Random Numbers</title>
      <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31223#M2481</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OIDs are not always sequential... Something like this (untested code) should work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;samplePct = 0.1
oidFieldName = arcpy.Describe(myFC).oidFieldName
oidList = [r[0] for r in arcpy.da.SearchCursor(myFC, ["OID@"])] #you could also use a feature layer here instead of a FC
sampleOidList = sorted(random.sample(oidList, int(len(oidList) * samplePct)))
sqlExp = oidFieldName + " in (" + ",".join([str(i) for i in&amp;nbsp; sampleOidList]) + ")"
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:15:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31223#M2481</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-10T21:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table of Random Numbers</title>
      <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31224#M2482</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Chris - Thank you for the reply and effort.&amp;nbsp; I tried sometime to make it work but am having difficulty.&amp;nbsp; What I am hoping for is to take my Summary Statistics output table and access this table in the script and use the fields within as inputs into the random.sample code and output the results into a dbf or gdb table.&amp;nbsp; The JOIN I can perform with just a simple arcpy.JOIN code later, but I really need assistance with the code below. The Summary Stats table contains one row with 6 fields, including these 4 I want to use further in the script: &lt;/SPAN&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;minimum object id&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;maximum object id&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;range&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;10% of the range&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;SPAN&gt;I know this won't work exactly as written, but I would think conceptually it would be something like the following (added to the code I've already written in original post).&amp;nbsp; I appreciate assistance on any of the following, thanks:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#Create parameters for the fields that already exist in the Summary Stats table...
minField = "MIN_OBJECTID"
maxField = "MAX_OBJECTID"
pctRange = "PERCENT_10"

#Use those parameters for input into the random sample generator...
random.sample(range(minField,maxField),pctRange)

#Write the result of the random sample list to a .dbf or gdb table&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:15:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31224#M2482</guid>
      <dc:creator>Corey_C_Denninger</dc:creator>
      <dc:date>2021-12-10T21:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table of Random Numbers</title>
      <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31225#M2483</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you have the Geostatistical Analyst extension you could use the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//003000000012000000"&gt;SubsetFeatures&lt;/A&gt;&lt;SPAN&gt; tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This will randomly split up your data and make a copy of what percentage of the data you requested (or you can request a number of values). It can also create a second data set which is all that is not in the first one.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This tools works against any type of feature class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Steve&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 May 2014 14:04:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31225#M2483</guid>
      <dc:creator>SteveLynch</dc:creator>
      <dc:date>2014-05-21T14:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table of Random Numbers</title>
      <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31226#M2484</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Wow, I did not know that Steve!&amp;nbsp; Thanks for the information.&amp;nbsp; I will try that out.&amp;nbsp; It would be nice to have this custom script tool as well.&amp;nbsp; If anyone out there is feeling particularly giving. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 May 2014 18:23:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31226#M2484</guid>
      <dc:creator>Corey_C_Denninger</dc:creator>
      <dc:date>2014-05-21T18:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table of Random Numbers</title>
      <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31227#M2485</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Corey&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is not a script tool. It is one of the geoprocessing tools that is shipped with ArcGIS. You do need a Geostatistical Analyst license however.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Steve&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 May 2014 20:25:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31227#M2485</guid>
      <dc:creator>SteveLynch</dc:creator>
      <dc:date>2014-05-21T20:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table of Random Numbers</title>
      <link>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31228#M2486</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Not sure if the trouble lies in:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1. Reading the min/max OID values from the input table (use a search cursor)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Calculating a 10% sample of the OID values from the range (see code below)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. Writing the sampled OIDs to an output table (use an insert cursor)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#2 might look like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;minOid = 1234
maxOid = 5678
samplePct = 0.1
rangeList = [i for i in range(minOid, maxOid + 1)]
sampleCount = int(len(rangeList) * samplePct + .5)
randomSampleList = random.sample(rangeList, sampleCount)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:15:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-table-of-random-numbers/m-p/31228#M2486</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-10T21:15:17Z</dc:date>
    </item>
  </channel>
</rss>

