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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MattBrady
    New Member
    • Aug 2012
    • 2

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

    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.


    Code:
    #demonstrates user defined range of possible random numbers.
    import random
    
    print ("\t\t\tWelcome th the Random # Generator v2.0")
    
    range1 = input ("\nPlease give the preffered minimum for the random number: ")
    range2 = input ("Please give the preffered maximum for the random number: ")
    
    num = random.rand (range1, range2)
    
    print ("Your number is ", num)
    Here is the error I got.

    Code:
    Traceback (most recent call last):
      File "C:/Python32/programs/usedDEFrand.py", line 9, in <module>
        num = random.rand (range1, range2)
    AttributeError: 'module' object has no attribute 'rand'
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You are close.

    Code:
    >>> random.randint(12,120)
    91
    >>>

    Comment

    • MattBrady
      New Member
      • Aug 2012
      • 2

      #3
      I was not looking for how to generate a random number within predefined range. im looking to get a user defined range.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Source code: Lib/random.py This module implements pseudo-random number generators for various distributions. For integers, there is uniform selection from a range. For sequences, there is uniform s...

        All that you want to know and so much more!

        :)

        -z

        Comment

        Working...