0

This is my current code:

import random while True: try: num = int(input("Enter the total number of digits you want to generate:")) upper = int(input("Enter your highest number:")) lower = int(input("Enter your lowest number:")) if upper < 1 or lower < 1 or upper <= lower: print ("Sorry, invalid. Make sure your upper bound is higher than lower bound, make sure all numbers are integers greater than 0.") continue if num < 1: print ("Sorry, invalid. Try again.") continue **mylist = [0] * num for x in range (lower, upper):** 

The part directly above here is the part that I am not quite sure how to go forward on. I want to generate random numbers depending on the number the user enters and within the upper and lower bounds that the user enters into a list that displays here. Can someone guide me through this? thank you!

except: print ("Sorry, invalid. Try again.") else: print ("Thank you.") break 
1
  • num, upper, and lower are either redundant or conflicting. Commented Dec 11, 2012 at 0:49

4 Answers 4

2

If you replace your ** lines with the following, using random.randint(lower, upper), you'll display the list you want:

mylist = [] for _ in range(num): mylist.append(random.randint(lower, upper)) print mylist 

You could also do this with a list comprehension:

mylist = [random.randint(lower, upper) for _ in range(num)] print mylist 
Sign up to request clarification or add additional context in comments.

2 Comments

thank you @mvchr, is there a way write the first code so that the same number does not repeat in the list more than once
@user1762229 see Brenden Brown's answer
1

you probably need:

print [random.randint(lower, upper) for n in range(num)] 

which displays a list of num random integers with minimum lower and maximum upper (inclusive).

Comments

0

You can use the randint to generate numbers in the given range.

Return a random integer x such that a <= x <= b.

import random print random.randint(a, b) 

So to generate N numbers in the range a and b

numbers = [random.randint(a, b) for i in range(N)] print numbers 

1 Comment

look at the random library. Don't forget to use random.seed().
0

If you wanted a random subset of the numbers between lower and upper, inclusive, you could use:

random.sample(num, xrange(lower, upper + 1))

http://docs.python.org/2/library/random.html#random.sample

But I think you wanted duplicates to be possible, in which case use one of the other answers.

3 Comments

That will generate num numbers, instead of num digits of random numbers. (The question may be badly worded, on this point.)
This generates a sequence with no repeats from the population. What if num is greater than upper-lower?
sampe gives unique numbers, no repeats, you probably need: print [random.randint(lower, upper) for n in range(num)] which displays a list of num random integer numbers with minimum lower and maximum upper (inclusive).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.