Field Calculator: Generate Random Numbers failure

10330
15
02-25-2011 06:08 AM
RichardThurau
Occasional Contributor
Hello,
I'm new to python so hopefully the solution is simple.

I am trying to generate random integers between 1 and 1000 to populate the field "Rand2".
The "Rand2" field is Float.


####
Pre-Logic Script Code:

import arcpy

# Set the randomGenerator environment to 4 MERSENNE_TWISTER
arcpy.env.randomGenerator = "4 MERSENNE_TWISTER"

# Calculate a random number using the arcgis.rand() function
result = arcpy.CalculateValue_management("arcgis.rand('INTEGER 1, 1000')")

# Get the value from the result object from CalculateValue and print
randomValue = float(result.getOutput(0))
print randomValue

Expression:
Rand2=

randomValue[!Rand2!]
####

Field calculator runs for a while then delivers an "Error running expression" message (See image for full message).



Can anyone tell me how to clean this up?

Thanks

Rich
Tags (2)
0 Kudos
15 Replies
DanPatterson_Retired
MVP Emeritus
did you try the second as well?  is your field an integer field?  worked on my computer
0 Kudos
RichardThurau
Occasional Contributor
Dan,
Thanks so much for keeping it up.

Well, looks like my error is data specific. I don't know why yet, but that code will not run on the dataset I've been trying to generate random numbers for. However, it did run perfectly on another shapefile (that has far fewer features?).

It ran on a Long field (I had been trying both Long and Float).

At least now I know that numpy at least works on my machine.

There used to be a way to mark issues as solved on these forums, but I'm not seeing that option right now.

Thanks for your help!

Rich
0 Kudos
DanPatterson_Retired
MVP Emeritus
Just a check if it is that particular shapefile...

1  File|Data Export Data give it a new name and saving location
2  try the code on the "clone"

or if you have some bad records

3  select the first couple of records in the table and try running the code on the selection
4  if that still doesn't work, select one record, export to a new file and try again.

if that doesn't work...no clue
0 Kudos
RichardThurau
Occasional Contributor
Dan,
I've messed around with a few things. The code runs on some datasets but not others (does not run on an exported copy of original). Neither file name length, nor the number of records seems to matter. It ran on some polygon shapefiles but not others.

The key for me was just to figure this code out because it's a tool I will be able to use in the future. Hopefully, when that time comes, I'll either have a dataset with which the code works, or I'll figure out why it doesn't.

But, till then, I'm moving on.

Thanks again for your help. I was going a little nuts there for a bit.

Rich
0 Kudos
JeffWard
Occasional Contributor III

For posterity, if you are hitting a data specific error, you can throw some debugging code in the script to see which records are giving you problems.  In this case, you could pass in the ObjectID to the field calculator function and put it in and arcpy.AddMessage note.  This way you would see how far things get before hitting the error.

Jeff Ward
Summit County, Utah
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
If you are still looking at this, I had luck with a slight variation of the code block given above:


(code block)
def rand_num():
  import random
  return random.randint(1,100)


then
(yourField) = rand_num()