Generate Random Number in Global Tag

Is there a simple way to generate a random number in a global tag, so that the tag can run by itself and generate random numbers for itself?

Initial Attempt:

  • Global script with function to generate random number
  • Call global script in runScript in expression
1 Like

In an expression tag, runScript("import random; random.randint(0, 1000)", 0) should work.

I am using the free and latest version of Ignition.

I tried that originally, but I get an error:

Error evaluating tag: Error parsing script for runScript() expression: SyntaxError: ("no viable alternative at input '='", ('expression:runScript', 1, 9, '__RESULT = import random; random.randint(0, 1000)\n'))

Ah, yeah, looks like the single line import syntax won’t work. Make a shared script, with a single defined function:

import random
def randint(lower, upper):
	return random.randint(lower, upper)

Then call it from your expression tag:
runScript("shared.agg.randint", 0, 0, 1000)

Thanks for the suggestion.

My bad that I didn’t mention that was my initial attempt. I update my question above.

I was hoping for something cleaner than this.

The trouble you are experiencing is due to the fact that runScript() is prefixing the string you feed it with an assignment statement to a hidden variable. So the code that actually executes in Paul’s first suggestion looks like this:

__someVar = import random; random.randint(0, 1000)

And then the __someVar value is fished out of the local variable table to feed to the binding. Not valid python.

If you add a real newline to your string expression, you can print dir() to see what that variable name is. You can also assign a constant temporarily, with a newline, import, execute anything, and assign a real return value several lines later.

1 Like

I am a little lost with what you are saying…could you please give an example?

Use Paul’s second suggestion.


Do you know what is going on with my project? I am following the instructions PGriffith posted in Oct. 2018 but for some reason I am still getting an "Error_ExpressionEval"

Is your Test script in the global scripting project?

In v7.9, back when this post was made, the shared.* namespace was the only collection of scripts that could be used by tags.

In v8+, that was replaced by the global scripting project setting, allowing more flexibility through the project inheritance system. But other projects, like v7.9's project.* namespace, are not callable from tags.

1 Like

Would you mind updating the instructions for 8.1?

I'm running 8.1.37 and can't get this to work using project.*


First, you must create a script/function in your defined gateway scripting project - this is key!

For example, I created this function inside of the script shared.util.calc:

def getRandom(lower = 0, upper = 100):
	Returns a random integer between lower and upper ranges.
		lower (int): Lower limit for random number
		upper (int): Upper limit for random number
		int: Random integer between lower and upper limits
	import random
	return random.randint(lower, upper)

Then my integer expression tag with an exectution mode set to fixed rate of 1000ms and the expression set to:

runScript('shared.util.calc.getRandom', 0, 0, 1000)

Returns a random integer between 0 and 1000.

If you call it with defaults it will return an integer between 0 and 100:

runScript('shared.util.calc.getRandom', 0)
1 Like

I'm not sure what I'm doing wrong, but I basically had the same thing you listed and got Error_ExpressionEval.

So then I did mine exactly the same as yours and am still getting the same error.

This is the diagnostic pic attached

Do you have the gateway scripting project set?

Shoot! I think that's my problem :man_facepalming:

This is a v7.9 topic. In the future, make sure you post in an appropriate topic, or start a new topic. I (and others, I expect) did not mention the global scripting project because it isn't applicable to v7.9. :frowning_face:

You mentioned it, but I saw other posts before I saw that "update for 8.1" comment.

My apologies. Thanks for the information. I will do that in the future