By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,799 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,799 IT Pros & Developers. It's quick & easy.

Help with random number generator? (I'm a beginner)

P: 2
Ok, so I just started learning python and I've made a simple random number generator already but now I want the user to define the range in which the number can be generated. This is what I have so far but I cant seem to figure out what to do for line 7. I'm guessing there's some way to convert rand1 and rand2 into integers.


Expand|Select|Wrap|Line Numbers
  1. #demonstrates user defined range of possible random numbers.
  2. import random
  3.  
  4. print ("\t\t\tWelcome th the Random # Generator v2.0")
  5.  
  6. range1 = input ("\nPlease give the preffered minimum for the random number: ")
  7. range2 = input ("Please give the preffered maximum for the random number: ")
  8.  
  9. num = random.rand (range1, range2)
  10.  
  11. print ("Your number is ", num)
  12.  
Here is the error I got.

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:/Python32/programs/usedDEFrand.py", line 9, in <module>
  3.     num = random.rand (range1, range2)
  4. AttributeError: 'module' object has no attribute 'rand'
Aug 25 '12 #1
Share this Question
Share on Google+
3 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
You are close.

Expand|Select|Wrap|Line Numbers
  1. >>> random.randint(12,120)
  2. 91
  3. >>> 
Aug 25 '12 #2

P: 2
I was not looking for how to generate a random number within predefined range. im looking to get a user defined range.
Aug 26 '12 #3

zmbd
Expert Mod 5K+
P: 5,397
http://docs.python.org/library/rando...#module-random
All that you want to know and so much more!

:)

-z
Aug 27 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.