# 'Import Random' Error

**URL:** <https://forum.inductiveautomation.com/t/import-random-error/17304>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [March 9, 2018, 9:46pm UTC](https://forum.inductiveautomation.com/t/import-random-error/17304 "2018-03-09T21:46:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan.powley](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jonathan.powley](https://forum.inductiveautomation.com/u/jonathan.powley)\
**Post date:** [March 9, 2018, 9:46pm UTC](https://forum.inductiveautomation.com/t/import-random-error/17304/1 "2018-03-09T21:46:08Z")

</div>

I’m trying use a Gateway Event Script to create a 16-digit randomly generated string every 10 seconds.

My script looks like:

```auto
import string
import random

def string_generator(size=16):
	return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(size))

system.tag.write('folder/tag',string_generator())

```

This works fine when testing in the Script Console. However, when I try to run it as a Gateway Event Script it does not.

The wrapper log shows the error:

```auto
INFO | jvm 1 | 2018/03/09 16:02:49 | E [c.i.i.c.s.TimerScriptTask] [21:02:49]: Error executing global timer script: HSES/randomString @10,000ms . Repeat errors of this type will be logged as 'debug' messages. 
INFO | jvm 1 | 2018/03/09 16:02:49 | com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO | jvm 1 | 2018/03/09 16:02:49 | File "<TimerScript:HSES/randomString @10,000ms >", line 8, in <module>
INFO | jvm 1 | 2018/03/09 16:02:49 | File "<TimerScript:HSES/randomString @10,000ms >", line 5, in string_generator
INFO | jvm 1 | 2018/03/09 16:02:49 | File "<TimerScript:HSES/randomString @10,000ms >", line 5, in <genexpr>
INFO | jvm 1 | 2018/03/09 16:02:49 | NameError: global name 'random' is not defined
INFO | jvm 1 | 2018/03/09 16:02:49 | 
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.Py.NameError(Py.java:260)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyFrame.getglobal(PyFrame.java:265)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.pycode._pyx864.f$2(<TimerScript:HSES/randomString @10,000ms >:5)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.pycode._pyx864.call_function(<TimerScript:HSES/randomString @10,000ms >)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyGenerator. __iternext__ (PyGenerator.java:149)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyGenerator. __iternext__ (PyGenerator.java:131)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PySequence.fastSequence(PySequence.java:241)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyString.str_join(PyString.java:1876)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyString$str_join_exposer. __call__ (Unknown Source)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyObject. __call__ (PyObject.java:391)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.pycode._pyx864.string_generator$1(<TimerScript:HSES/randomString @10,000ms >:5)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.pycode._pyx864.call_function(<TimerScript:HSES/randomString @10,000ms >)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyBaseCode.call(PyBaseCode.java:114)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyFunction. __call__ (PyFunction.java:307)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.pycode._pyx864.f$0(<TimerScript:HSES/randomString @10,000ms >:11)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.pycode._pyx864.call_function(<TimerScript:HSES/randomString @10,000ms >)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.PyCode.call(PyCode.java:18)
INFO | jvm 1 | 2018/03/09 16:02:49 | at org.python.core.Py.runCode(Py.java:1275)
INFO | jvm 1 | 2018/03/09 16:02:49 | at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:634)
INFO | jvm 1 | 2018/03/09 16:02:49 | at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:601)
INFO | jvm 1 | 2018/03/09 16:02:49 | at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:88)
INFO | jvm 1 | 2018/03/09 16:02:49 | at java.util.TimerThread.mainLoop(Unknown Source)
INFO | jvm 1 | 2018/03/09 16:02:49 | at java.util.TimerThread.run(Unknown Source)
INFO | jvm 1 | 2018/03/09 16:02:49 | Caused by: org.python.core.PyException: Traceback (most recent call last):
INFO | jvm 1 | 2018/03/09 16:02:49 | File "<TimerScript:HSES/randomString @10,000ms >", line 8, in <module>
INFO | jvm 1 | 2018/03/09 16:02:49 | File "<TimerScript:HSES/randomString @10,000ms >", line 5, in string_generator
INFO | jvm 1 | 2018/03/09 16:02:49 | File "<TimerScript:HSES/randomString @10,000ms >", line 5, in <genexpr>
INFO | jvm 1 | 2018/03/09 16:02:49 | NameError: global name 'random' is not defined

```

Any information explaining why `import random` would not be working would be appreciated.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 9, 2018, 9:52pm UTC](https://forum.inductiveautomation.com/t/import-random-error/17304/2 "2018-03-09T21:52:05Z")

</div>

The scope changes inside the function. You’ll need to import it there, instead.

---

<div class="post-metadata">

**Author:** ![jonathan.powley](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jonathan.powley](https://forum.inductiveautomation.com/u/jonathan.powley)\
**Post date:** [March 9, 2018, 9:55pm UTC](https://forum.inductiveautomation.com/t/import-random-error/17304/3 "2018-03-09T21:55:46Z")

</div>

That worked. Thank you sir.
