1
import random

def main():
    uinput()

def uinput():
    times = int(input('How many numbers do you want generated?'))
    number1 = int(input('Put in the first number:' ))
    number2 = int(input('Put in the second number:'))
    rnumber = random.randint(number1, number2)
    print (rnumber)



main()

I am messing around in Python, and I want my program to generate random numbers. As you can see I have already accomplished this. The next thing I want to do is have it generate multiple random numbers, depending on how many numbers the "user" wants generated. How would I go about doing this? I am assuming a loop would be required.

3
  • 3
    So read about loops, write a loop, and report back! Commented Nov 5, 2013 at 0:13
  • if number1>number2 you will get ValueError Commented Nov 5, 2013 at 0:15
  • If you know the range you want to pick from, you can also use random.choices(range, k=n), where n is the number of numbers to yield. Commented Sep 27, 2019 at 14:38

6 Answers 6

8

This will produce a list of random integers in range number1 to number2

rnumber = [random.randint(number1, number2) for x in range(times)]

For more information, look at List Comprehension.

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

Comments

2

I would recommend using a loop that simply adds to an existing string:

import random
from random import randint
list=""
number1=input("Put in the first number: ")
number2=input("Put in the second number: ")
total=input("Put in how many numbers generated: ")
times_run=0
while times_run<=total:
    gen1=random.randint(number1, number2)
    list=str(list)+str(gen1)+" "
    times_run+=1
print list

Comments

1

Use a "for" loop. For example:

for i in xrange(times):
    # generate random number and print it

Comments

1

Use generators and iterators:

import random
from itertools import islice

def genRandom(a, b): 
    while True:
        yield random.randint(a, b)


number1 = int(input('Put in the first number:' ))
number2 = int(input('Put in the second number:'))
total = int(input('Put in how many numbers generated:'))

rnumberIterator = islice(genRandom(number1, number2), total)

Comments

0

You can define a function to take user input to generate numbers in a given range. You can return the function or print the function.

import random

def ran_nums():
    low_range = int(input('Enter the low end of the range: '))
    high_range = int(input('Enter the high end of the range: '))
    times = int(input('How many numbers do you want to generate? '))
    for num in range(times):
        print(random.randint(low_range,high_range))

Comments

-2

This code chooses a random number:

import random
x = (random.randint(1, 50))
print ("The number is ", x)

Repeat the code 6 times to choose 6 random numbers.

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.