ArcGIS Toolbox – Generate Random Points with ArcPy

Share Button

Updated – 2/27/2015
Fixed compatibility with Python 3 for ArcGIS Pro

(If you’re using Firefox, then right click and Save Link As)
Download the Generate Random Points Toolbox – works with ArcGIS 10.1+
View the Source Code

Maybe you’re doing a soil analysis study (check out my Create Custom Grid tool) and need random locations for each sample? Or you’re doing a tree diversity study and need random locations where the number of tree species will be identified? Or maybe you’re just looking for an adventure and want to visit each random location created from a polygon around a city neighborhood? Who knows…

The Generate Random Points tool generates a number of random points inside each polygon of an input Polygon feature class. The number of random points can be a fixed number or based on a field.

An optional parameter can be set to define a minimum distance between the random points. There is also a parameter called Maximum Attempts, which is the maximum attempts the tool will make before failure. Depending on the number of random points and the minimum distance, there might not be a possible solution so the tool would run indefinitely.

If the maximum attempts for a polygon is reached, there are 3 choices on how to handle:

NO – The tool will fail and no random points will be generated.
KEEP_ALL – All generated random points will be returned. If one polygon could only generate 20 out of 100 random points, the 20 points will be included in the output.
KEEP_ONLY_SUCCESSFUL – Only polygons that succesfully generated all the required random points will have the random points included in the output.

The output Point feature class will contain a field called PolygonOID, which states the OID of the Polygon that each point was generated from.

Be sure to view the Tool Help within the tool dialog box for detailed instructions. Also, check the Geoprocessing Results for any error messages.

A similar ESRI tool called Create Random Points requires ArcInfo (Advanced) licensing.
This tool only requires ArcGIS 10.1 or later at any licensing level.

Here’s a screenshot of the parameters:
Generate Random Points params

Here’s an example of generating 20 random points:
Generate Random Points

Here’s an example of generating 20 random points with a minimum distance of 200 feet:
Generate Random Points Min Distance

I hope someone can find this tool useful. Please leave a comment if you have any questions or problems, and be sure to check out the other tools I’ve created!

19 thoughts on “ArcGIS Toolbox – Generate Random Points with ArcPy

  1. Michelle Fults
    September 19, 2014 at 1:55 pm

    I get the following error message. I am not a programmer so I am not sure how to fix this. I have numerous small polygons or clusters that I need to select a random number of these based on the size of the polygon.

    Executing: GenerateRandomPoints MS4_CC_LynchBlksburg_091914 NO # 20 0 2000 NO C:\MichelleGISData\MS4_Annual2014\LynchBlkburg_1pctRandom_091914.shp
    Start Time: Fri Sep 19 13:54:10 2014
    Running script GenerateRandomPoints…
    SyntaxError: invalid syntax (, line 1)
    Failed to execute (GenerateRandomPoints).
    Failed at Fri Sep 19 13:54:47 2014 (Elapsed Time: 37.00 seconds)


    1. Ian
      September 22, 2014 at 5:52 pm

      Michelle- thanks for letting me know! It looks like I accidentally uploaded a bad copy of the toolbox. I’ve put up a new working copy.

    2. Dean
      August 20, 2015 at 7:07 pm

      This is a fantastic tool. Would be awesome to have it so that minimum distance is based on a field too

      1. Ian
        September 6, 2015 at 12:08 am

        Hi Dean, thanks for the comment! That really wouldn’t be hard to do. If I get the time, I’ll try to add that capability!

        1. Anonymous
          September 28, 2015 at 7:23 pm

          Thanks Ian

  2. Jodi
    November 11, 2014 at 6:43 pm

    Worked like a charm! Thanks so much!

    1. Ian
      November 11, 2014 at 8:43 pm

      Great, thanks for the comment!

  3. Luke
    December 8, 2014 at 7:52 am

    Your scripts have come in incredibly handy. I really appreciate you making them available. It is especially helpful to have the scripts available to be able to learn just how they work in process. Thanks again!

    1. Ian
      December 11, 2014 at 6:57 pm

      Thanks, Luke!

  4. March 19, 2015 at 8:22 am

    Hey Ian, Great site and great tools. Just a little discussion point that I got thinking about while looking at this.

    For the minimum distance setting “Keep_Successful”, does this just generate a random set of coords and then test if it is less than that minimum distance to another point? Inserting it if not and trying another random point again until the max number of tries?

    It just feels like it lacks a bit of robustness if you get a lot less points than you wanted because you were unlucky with the ‘random’ part. You mention to lower the min distance or try more attempts but even keepin he same could result in different number of points.

    As I write this I have a few ideas but none would be easily implemented or be perfect – using knowledge of the existing points locations, the max points that could fit in the polygon with the given min distance and statistical variance in the spread of the given number of points against the area and min distance.

    Don’t mean to criticize, just wondered if you had any thoughts on it and if you tried anything more elaborate? The tool is great as is and for 99% of purposes wouldn’t need this functionality anyway. Purely academic.

    1. March 19, 2015 at 8:25 am

      That’s creepy. Where did the picture of me come from? I didn’t upload anything.

      1. Ian
        March 20, 2015 at 1:09 am

        It’s from Gravatar. The email you used must have an account there.

  5. Naomi
    July 10, 2016 at 10:22 pm

    Hi Ian,
    Thanks another useful tool 🙂

    Would you be able to add in an option to carry all the fields from the original polygon layer?

    1. Naomi
      July 12, 2016 at 3:35 pm

      I’ve found another way to create points live within my SQL query. I haven’t figured out how to make it generate random points though.

    2. Ian
      August 30, 2016 at 6:00 pm

      Hi Naomi, thanks for the comment! I should be able to make that change sometime in the future. I’ve been busy lately, so I’m not sure when it will be. I’ll email you when I do.

  6. Silvio Gomes
    November 7, 2016 at 10:16 am

    Hi. The proposal of the tool is amazing. Perhaps, I have the follow message when I try to generate six points to my polygon: ‘Polygon’ object has no attribute ‘spatialReference’
    Viewing the polygon properties and coordinate system of the shape, the spatial reference sounds ok. What should I do?

    1. Ian
      November 7, 2016 at 6:50 pm

      Hi Silvio, I’ve sent you an email to get more information.

  7. Clueless
    December 5, 2016 at 10:40 am

    Basic question. After I have downloaded the GenerateRandomPoints file.tbx, what next? Double clicking the file gives me a message that no program is associated with the file.

    ArcGIS 10.3, Windows 7

    1. Ian
      January 5, 2017 at 10:25 pm

      Hello, I’ve sent you an email.

Leave a Reply

Your email address will not be published.