27

I'm trying to produce a random integer n, and create a list of n random integers with values between 0 and 9.

Here is my code:

def randomNumbers(n):
    myList = []
    needMoreNumbers = True
    while (needMoreNumbers):
        randomNumber = int(random.random() * 10)
        myList.append(randomNumber)
        n = n -1
        if (n < 1):
            needMoreNumbers = False
    return myList

When I run it, it says:

NameError: global name 'random' is not defined
2
  • 7
    Did you remember to import random ? Commented Sep 10, 2013 at 19:47
  • Why don't you accept the answer? Commented Sep 5, 2017 at 11:59

2 Answers 2

60

You haven't imported random module. Add this to the top of your script:

import random 
Sign up to request clarification or add additional context in comments.

2 Comments

what does the import function do? thank you for your help by the way. I got it to work.
Take a look at python docs on modules and import.
5

The script file name can't be "random.py", doing this will trigger a circular import making your program fail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.