Field Calculator: Generate Random Numbers failure

10336
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
if getoutput returns a list, then this line
randomValue = float(result.getOutput(0))

should be

randomValue = float(result.getOutput[0])
0 Kudos
RichardThurau
Occasional Contributor
Thanks Dan,

I made the change. But, still not functioning. Now error message is:

<type 'exceptions.TypeError'>: 'instancemethod' object is unscriptable.

Progress?

not sure

Rich
0 Kudos
DanPatterson_Retired
MVP Emeritus
so it isn't returning a python list, however, looking at the help files and your example I noted

'INTEGER 1, 1000' was your case
and in the help files at
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001w0000000w000000.htm
it was
'normal 0.0 10.0'
the only difference between them, excluding the distribution type, was that you had a pesky "," after the 1 in yours and the help files seems to suggest that a space delimited string is used.  Barring that, try the help file example to rule out further issues.
0 Kudos
RichardThurau
Occasional Contributor
Yes, I've been trying all sorts of variations based on that example and what I can read about the function. From what I can see in the error messages, commas and capitalizations don't seem to matter too much.

I did try the tool originally using the provided script exactly as presented.

Either the documentation is wrong for arcgis.rand(), the documentation is leaving out some vital steps, I'm doing something wrong that no one has pointed out, or arcpy is screwed up on my machine.

Still trying though.

RT
0 Kudos
DanPatterson_Retired
MVP Emeritus
instead of
randomValue[!Rand2!]
try
randomValue(!Rand2!)
0 Kudos
RichardThurau
Occasional Contributor
You are a trooper Dan.

That was one of the first things I noticed between the code example in help, and the code examples that I saw others post.

I just tried it again anyway, with not success. I even got rid of defining the distribution. Current code block is:

import arcpy

# Calculate a random number using the arcgis.rand() function
result = arcpy.CalculateValue_management("arcgis.rand('normal 0.0 10.0')")

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

Expression is:
val(!Rand2!)


I had to move on, so I generated a random table of values on Random.org's website, pasted into excel, joined the excel file, and used field calculator to copy the random values. That's a lot of work for what should be a simple code.

Of course I wouldn't spend so much time if I didn't think this was going to helpful to me in the future.

Therefore, i would greatly appreciate any attempts to find a fix.

Thanks a lot.

Rich
0 Kudos
DanPatterson_Retired
MVP Emeritus
Rich
I will look into it some more, try the code block that uses numpy's random in the interim
0 Kudos
DanPatterson_Retired
MVP Emeritus
stupids finally over

parser:  Python

Code block
import numpy
def rand_num(a_field):
  return  numpy.random.randint(1,1000)

the rest

rand_num(!randnum!)
where !randnum! is the field to place the random numbers.  I think you were mixing up the script example with the field calculator example and I just didn't catch it

or better still provide a minimum and maximum value
import numpy
def rand_num(min_val, max_val):
  return  numpy.random.randint(min_val,max_val)


with
rand_num(1,1000)

calling the function
0 Kudos
RichardThurau
Occasional Contributor
Well, this isn't very clear to me. I tried both examples you've given here. At least in a way I am interpreting. ArcGIS doesn't want to calculate either of them.

Like I said, I'm really new to using python, and I haven't successfully gotten anything to run from Filed Calculator yet.

I guess the question is, does this code work on your machine? I'm starting to think there could be something wrong with python or something on my machine.

I've attached an image of the code I'm trying to populate my Rand2 field with.

RT
0 Kudos