import random
n = random.random() #returns a random float between 0 and 1
print(n)
n = random.randint(0, 9) #returns a random integer betweem 0 and 9.
print(n)
0.8734221981946652
5
We need to import
the module random
What do the functions .random()
and .randint()
return?
pick_winner
What happens when you run pick_winner
multiple times?
To get always the same result (for autograding purposes, for example) we can set a seed.
Write a function random_list
that takes as argument a list of numbers
. Iterate over each list element (with a while
loop), replacing each integer with a random number between zero and the original number. Set the seed as 123
in your function.
Name your file random_list.py
and submit to Gradescope.
import random
def random_list(numbers):
random.seed(123)
index = 0
while index < len(numbers):
item = numbers[index]
numbers[index] = random.randint(0, item)
index += 1
return numbers
if __name__ == "__main__":
assert random_list([3, 2, 1, 3, 5]) == [0, 1, 0, 3, 2]
print("pass the test")
pass the test