Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Within a larger ArcGIS ModelBuilder model I am trying to populate a field in an attribute table with a random number, using Calculate Value. The overall distribution of random numbers needs to follow a Normal distribution. The parameters of that normal distribution (mean and standard deviation) need to be calculated from model variables specified by the user. Is this possible?

The arcgis.rand() function can do what I want and works perfectly if I specify the mean and stdev directly in the Calculate Value expression:

arcgis.rand("Normal 6 2.5") 

If I use model variables directly, it also works:

arcgis.rand("Normal %mean% %stdev%")

However, in some instances I need to do some simple calculations to obtain the parameters from variables the user has entered. For example, they have already entered variables A, B and C, and I need the mean to be A*B and the stdev to be B/C.

I have attempted to do this via the code block:

import random
MN = %A%*%B%
SD = %B%/%C%

and then the expression

arcgis.rand("Normal MN SD")

but this always results in values following the default distribution (mean 0, stdev 1). Troubleshooting shows this happens even if I define the code block variables as simple numbers, suggesting the problem seems to be using code block variables in the expression, not in the definition or calculation of the variables themselves.

My question is: how can I do what I need using arcgis.rand()?

I am open to suggestions of other methods/workflows for achieving what I need, but I am a novice at Python, in case that is not already obvious! I am using ArcGIS Desktop Advanced 10.2.2.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You're using your variable names in a string literal. You can do the following to have the values substituted into the string:

arcgis.rand("Normal {0} {1}".format(MN, SD))

You could also import numpy and use numpy.random.normal(MN, SD, n) to draw a sample of size n from the specified normal distribution. Documentation.

share|improve this answer
    
OK, so to be sure I understand correctly: the core issue is that I cannot directly use variables defined in the code block (MN, SD) because they need to go within the quotation marks in the Calculate Value expression. However, I can use string formatting to create the expression and include my variables. (As a note for future readers, I found docs.python.org/2/library/string.html very useful). Alternatively, because 'numpy' doesn't need quotation marks, my variables work fine there without needing string formatting. I used the first solution and it works. Thank you @nmpeterson! –  ermintrude75 Aug 7 at 14:21
    
Yes, Esri has chosen an odd, un-pythonic way to parameterize distributions by using a space-delimited string. I'm not sure why, but nothing surprises me anymore when it comes to Esri. –  nmpeterson Aug 7 at 14:28

I have not used arcgis.rand which I found documented under the Calculate Value tool.

However, there seems to have been a bug NIM099744 found in ArcGIS 10.2.1 for Desktop:

In ArcGIS 10.2.1, the arcgis.rand function incorrectly gives only one value as an output instead of random values.

Perhaps this is what you are encountering.

share|improve this answer
    
Thank you for replying, but I can confirm this is not what I am experiencing. A range of values is created, but following the default normal distribution not the one I am trying to specify using variables. (I had not mentioned my ArcGIS version and will edit the question now.) –  ermintrude75 Aug 7 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.