Reputation: 187
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
Upvotes: 0
Views: 244
Reputation: 2033
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]
Upvotes: 1
Reputation: 5896
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]
Upvotes: 0