Exercises
Random Numbers
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Seed twice with the same number and show the sequences match.
Python
import random
# print True
Seed, generate, seed again, generate, compare.
import random
random.seed(42)
first = [random.randint(1, 100) for _ in range(5)]
random.seed(42)
second = [random.randint(1, 100) for _ in range(5)]
print(first == second)Exercise 2Passed
Deal three different cards from the deck.
Python
import random
deck = ["a", "b", "c", "d"]
# take three with no repeats, then print how many are unique
One of choices and sample avoids repeats.
import random
deck = ["a", "b", "c", "d"]
hand = random.sample(deck, 3)
print(len(set(hand)))Exercise 3Passed
Generate a token safely. Print its length.
Python
# print 32
random is not safe for this. There is a module built for it.
import secrets
print(len(secrets.token_hex(16)))