# Generate Random Number in Global Tag

**URL:** <https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [October 23, 2018, 9:00pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334 "2018-10-23T21:00:14Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexanderfalinsky](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@alexanderfalinsky](https://forum.inductiveautomation.com/u/alexanderfalinsky)\
**Post date:** [October 23, 2018, 9:00pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/1 "2018-10-23T21:00:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 23, 2018, 9:13pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/2 "2018-10-23T21:13:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alexanderfalinsky](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@alexanderfalinsky](https://forum.inductiveautomation.com/u/alexanderfalinsky)\
**Post date:** [October 23, 2018, 9:17pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/3 "2018-10-23T21:17:48Z")

</div>

> [@paul-griffith](#):
>
> runScript("import random; random.randint(0, 1000)", 0)

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'))

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 23, 2018, 9:23pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/4 "2018-10-23T21:23:08Z")

</div>

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

```auto
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)`

---

<div class="post-metadata">

**Author:** ![alexanderfalinsky](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@alexanderfalinsky](https://forum.inductiveautomation.com/u/alexanderfalinsky)\
**Post date:** [October 23, 2018, 9:25pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/5 "2018-10-23T21:25:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 23, 2018, 9:48pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/6 "2018-10-23T21:48:18Z")

</div>

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:

```auto
__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.

---

<div class="post-metadata">

**Author:** ![alexanderfalinsky](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@alexanderfalinsky](https://forum.inductiveautomation.com/u/alexanderfalinsky)\
**Post date:** [October 23, 2018, 9:54pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/7 "2018-10-23T21:54:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 24, 2018, 1:21am UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/8 "2018-10-24T01:21:53Z")

</div>

Use Paul’s second suggestion.

---

<div class="post-metadata">

**Author:** ![jfchicat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jfchicat/32/63831_2.png) [@jfchicat](https://forum.inductiveautomation.com/u/jfchicat)\
**Post date:** [June 15, 2024, 12:30pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/9 "2024-06-15T12:30:14Z")

</div>

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"

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/0/b01715f74f6fe41fde6e0225820343c889fd59f5.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 15, 2024, 12:50pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/10 "2024-06-15T12:50:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![keith.lyding](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@keith.lyding](https://forum.inductiveautomation.com/u/keith.lyding)\
**Post date:** [July 23, 2024, 2:40pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/12 "2024-07-23T14:40:37Z")

</div>

Would you mind updating the instructions for 8.1?

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

Thanks

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 23, 2024, 2:59pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/13 "2024-07-23T14:59:17Z")

</div>

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`:

```python
def getRandom(lower = 0, upper = 100):
	'''
	Returns a random integer between lower and upper ranges.
	
	Args:
		lower (int): Lower limit for random number
		upper (int): Upper limit for random number
		
	Returns:
		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:

```python
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:

```python
runScript('shared.util.calc.getRandom', 0)

```

---

<div class="post-metadata">

**Author:** ![keith.lyding](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@keith.lyding](https://forum.inductiveautomation.com/u/keith.lyding)\
**Post date:** [July 23, 2024, 5:03pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/14 "2024-07-23T17:03:16Z")

</div>

> [@michael.flagler](#):
>
> ```python
> def getRandom(lower = 0, upper = 100):
> '''
> Returns a random integer between lower and upper ranges.
> 	
> Args:
> lower (int): Lower limit for random number
> upper (int): Upper limit for random number
> 		
> Returns:
> int: Random integer between lower and upper limits
> 		
> '''
> import random
> return random.randint(lower, upper)
> 
> ```

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/4/54aaba31a96bde3d12089de68be14bede1ee5de7.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 23, 2024, 5:24pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/16 "2024-07-23T17:24:30Z")

</div>

**Do you have the gateway scripting project set?**

> **[Project Library | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/project-library#gateway-scripting-project)**
>
> Scripts under the Project Library are a project-based resource that allows user created Python scripts to be configured. Objects and functions created in a Project Library script can be called from anywhere in the project. Project Library scripts are...

---

<div class="post-metadata">

**Author:** ![keith.lyding](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@keith.lyding](https://forum.inductiveautomation.com/u/keith.lyding)\
**Post date:** [July 23, 2024, 5:26pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/17 "2024-07-23T17:26:29Z")

</div>

Shoot! I think that's my problem 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 23, 2024, 5:29pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/18 "2024-07-23T17:29:08Z")

</div>

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. ☹

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

---

<div class="post-metadata">

**Author:** ![keith.lyding](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@keith.lyding](https://forum.inductiveautomation.com/u/keith.lyding)\
**Post date:** [July 23, 2024, 5:31pm UTC](https://forum.inductiveautomation.com/t/generate-random-number-in-global-tag/20334/19 "2024-07-23T17:31:40Z")

</div>

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