As the title says, I need help on how to generate random numbers.
Like say, if the program asks the user "How many random numbers do you want?" and the user enters "4" It would generate 4 random numbers.

How do I do that?

the programs outputs random numbers on how many the user wants.

You really should've tried google

But since this is such a simple request here:

>>> import random
>>> random.randint(0, 100)
20
>>>

There are many great functions within the random module... take a look at the documentation for examples of how to use them.

You really should've tried google

But since this is such a simple request here:

>>> import random
>>> random.randint(0, 100)
20
>>>

There are many great functions within the random module... take a look at the documentation for examples of how to use them.

Okay, I get that part, but I'm not sure how to do the "output the number of random numbers the user wants."

Use a for loop, ie

usr_inp = User's Input
for each in usr_inp:
    generate_number

Okay. Here is what i've done. Can you help me edit this? Keeps giving me a error.

import random

user_input = int(raw_input("How many random numbers do you want?\n"))
for each in user input:
    randomnumber = random.randrange(20)+1
    print randomnumber

Since user_input is a number, it is not an iterable item (ie, list, dictionary, string, etc). In order to get an iterable number use range() or xrange(). In Python 2.X xrange is preferrable; however in Python 3.0 xrange will be the new range.

so you'll do:

for each in xrange(user_input):
## On Python 2.X installation

Since user_input is a number, it is not an iterable item (ie, list, dictionary, string, etc). In order to get an iterable number use range() or xrange(). In Python 2.X xrange is preferrable; however in Python 3.0 xrange will be the new range.

so you'll do:

for each in xrange(user_input):
## On Python 2.X installation

omg, thank you!

Oh, thank you. Now I wanted to make this repeatable, and exitable. But the "Do you want to repeat this? (y=yes, n=no)" Is at the start.. it should be after the def dieroll() , how do i fix it, do you know?

import random

def dieroll(): 
 user_input = int(raw_input("How many random numbers do you want?\n"))
 for each in xrange(user_input):
    randomnumber = random.randrange(20)+1
    print randomnumber

answer=raw_input("Do you want to repeat this (y=yes, n = no)?")

if "y" in answer:
       dieroll()
else:
        print " okay bye!"

raw_input("\n\nPress the enter key to exit.")
import random

def dieroll(): 
 user_input = int(raw_input("How many random numbers do you want?\n"))
 for each in xrange(user_input):
    randomnumber = random.randrange(20)+1
    print randomnumber

answer = 'y'
while answer == 'y':
    dieroll()
    answer=raw_input("Do you want to repeat this (y=yes, n = no)?")

print " okay bye!"
raw_input("\n\nPress the enter key to exit.")

Do you understand what I did?

oh.. yea.. i get it .
Instead of using the 'if else elif' statement you just did the "answer=__" while answer thing. Thanks alot.

This article has been dead for over six months. Start a new discussion instead.