Tsallaka zuwa abun ciki
Justice Tech School

The random Module

random.random() gives a float from 0 to 1, randint(a, b) a whole number including both ends, choice picks one item, shuffle mixes a list and sample picks several without repeats. Setting random.seed() makes the results repeatable. These are not secure: use the secrets module for passwords.

Example
import random
random.seed(42)
print(random.randint(1, 10))
print(random.choice(["red", "green", "blue"]))
cards = [1, 2, 3, 4, 5]
random.shuffle(cards)
print(cards)
print(random.sample(range(100), 3))

Result

2
red
[4, 5, 1, 2, 3]
[94, 13, 86]

Exercise

Pick one item from a list.

random.(["a", "b"])

Quiz Test

Which module should create passwords?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.