Python Practice › List Programs › Program 58

Pick random numbers from a list

EasyList Programs · 58 of 100

Write a Python program to select 4 random numbers from a list.

Advertisement
Input
[1, 4, 5, 7, 3, 2, 9]
Output
[3, 5, 2, 1]
9

Approach

  1. random.sample picks without repeats; seed for repeatable output.

Solution

Python
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.

More list programs

← All 100 List Programs

Leave a Comment