0

The question is to write a function called randNumMaxFor(n, maxValue), with a for loop that generates a list of n random numbers between 0 and maxValue. I can get the random list generated with a for loop but I have no clue how to get to the next step. Thanks for any hints or tips.

import random
def randomNumbers(n):    
    #create an empty list
    myList=[]

    #While loop to create new numbers
    for n in range(0,9):
        #create a random integer between 0 and 9, inclusive
        randomNumber=int(random.random()*10)

        #add the random number to the list of random numbers using the append() method
        myList.append(randomNumber)

    return myList
2
  • What's the 'next step'? Commented Sep 6, 2015 at 21:49
  • What happens to the n you pass in as a parameter? It's re-used in the for loop. Commented Sep 6, 2015 at 21:50

2 Answers 2

1

for loop that generates a list of n random numbers between 0 and maxValue

Your loop doesn't iterate from 0 to n. Replace with this:

for i in range(0,n):

You can use randint() to generate random numbers between 0 to maxValue. It will be something like randint(0, maxValue).

You can then sort the list and return the last value which will be the max value

return sorted(myList)[len(myList) - 1]
Sign up to request clarification or add additional context in comments.

Comments

0
import random
def randNumMaxFor(n, maxValue):
    ret = []
    for i in range(n):
        ret.append(random.random() * maxValue)
    return ret

>>> randNumMaxFor(5, 100)
[26.72290088458923,
 59.19115828283099,
 92.35251365446278,
 21.0800837007157,
 44.83968075845669]

2 Comments

you can use a list comprehension here: return [random.random() * maxValue for _ in range(n)]
Yes, I just assumed it was a homework task and used more simple syntax.

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.