Python Practice › List Programs › Program 58
Pick random numbers from a list
Write a Python program to select 4 random numbers from a list.
Input
[1, 4, 5, 7, 3, 2, 9]
Output
[3, 5, 2, 1] 9
Approach
random.samplepicks without repeats; seed for repeatable output.
Solution
import random random.seed(5)numbers = [1, 4, 5, 7, 3, 2, 9]print(random.sample(numbers, 4))print(random.choice(numbers))Output
[3, 5, 2, 1] 9
Learn the concept
New to this topic? Read the lesson, then come back.