processing a random.sample

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilsn0opy04
    New Member
    • Mar 2007
    • 1

    #1

    processing a random.sample

    I am pretty new to Python. I wanted to create a program so that if a user entered a number, then that amount of random numbers will appear.

    I wanted my random.randrang e(100)+1

    "How many numbers do you want to generate?" 4
    19
    2
    45
    35

    Also I wanted to know how to find the
    - average to the random numbers
    - tell which ones are even
    - tell which ones are odd, divisible by3, and greater than 50

    Can anyone PLEASE help me to any part of this? THank you so much!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by lilsn0opy04
    I am pretty new to Python. I wanted to create a program so that if a user entered a number, then that amount of random numbers will appear.

    I wanted my random.randrang e(100)+1

    "How many numbers do you want to generate?" 4
    19
    2
    45
    35

    Also I wanted to know how to find the
    - average to the random numbers
    - tell which ones are even
    - tell which ones are odd, divisible by3, and greater than 50

    Can anyone PLEASE help me to any part of this? THank you so much!
    To get the input, use:
    Code:
    countStr = raw_input("How many numbers do you want to generate?")
    try:
        count = int(countStr)
    except ValueError:
        count = 0
    To get a list of random numbers between 0 and 99:
    Code:
    import random
    randList = random.sample(range(100), count)
    To get the average:
    Code:
    avg = sum(randList) / len(randList)
    To count the number of 'things':
    Code:
    i = 0
    for item in randList:
        if item > 50:
            i += 1
    print "items greater than 50 =", i
    Let's see if you can do some on your own. There's help here if you need it.

    Comment

    Working...