Assigning random numbers to points

3137
3
11-23-2011 09:26 AM
by Anonymous User
Not applicable
Original User: cdeaner66

Hi,

I need to assign random numbers to point features.  I have tried using the calculate field tool using the following expression arcgis.rand("Integer 1 16") but rather than assign the features the numbers 1-16 it randomly distributes the values so I may end up with no points with the number 3 and 2 points with the number 9 for example.

Does anyone know how to modify this code or use a different expression to randomly assign the points a number?

Thanks,
Chris
0 Kudos
3 Replies
by Anonymous User
Not applicable
Original User: Sycosys

# Calculate a field to have random values derived from a uniform distribution of integers between 0 and 16


import arcgisscripting, sys
gp = arcgisscripting.create()

# An input feature class

inputFC = sys.argv[1]

gp.AddField_management(inputFC, "Value", "LONG")
gp.CalculateField_management(inputFC, "Value", "arcgis.rand('Integer 0 16')", "PYTHON", "#")
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Chris,

I was able to get the following to work w/o returning any duplicates:

Pre-logic Script
def update():
  import arcpy, random
  mylist = []
  x = 1
  while x <= 16:
    mylist.append(x)
    x += 1
  x = 1
  rows = arcpy.UpdateCursor(r"C:\temp\python\test.gdb\Fires")
  for row in rows:
    y = random.choice(mylist)
    row.Sample = y
    val = row.Sample
    mylist.remove(y)
  del row, rows
  return val
0 Kudos
by Anonymous User
Not applicable
Original User: JSkinn3

Hi Chris,

I have not tried in the field calculator, but I was able to get the following code to work and it did not return any duplicates:

import arcpy, random

mylist = []

x = 1

while x <= 16:
    mylist.append(x)
    x += 1

rows = arcpy.UpdateCursor(r"C:\temp\python\test.gdb\Fires")

for row in rows:
    y = random.choice(mylist)
    row.Random = y
    rows.updateRow(row)
    mylist.remove(y)

del row, rows
0 Kudos