Python Random Module Quiz
A 35-question quiz covering random number generation, selecting elements, shuffling sequences, and managing seeds.
Question 1
What is the primary function of the `random` module in Python?
Question 2
Analyze the code below. What is the possible range of values for `x`?
import random
x = random.randint(1, 10)Question 3
Which function returns a random floating-point number in the range [0.0, 1.0)?
Question 4
Analyze the code below. What happens to `my_list`?
import random
my_list = [1, 2, 3, 4]
random.shuffle(my_list)Question 5
What is the purpose of `random.seed(a)`?
Question 6
Which function would you use to pick a single random element from a non-empty sequence?
Question 7
Analyze the code below. What is the difference between `randint(1, 10)` and `randrange(1, 10)`?
import random
a = random.randint(1, 10)
b = random.randrange(1, 10)Question 8
How do you select 3 unique elements from a list `population`?
Question 9
Analyze the code below. What will be the output?
import random
random.seed(42)
print(random.random())
random.seed(42)
print(random.random())Question 10
Which function returns a random float N such that `a <= N <= b`?
Question 11
What happens if you call `random.choice([])` (an empty list)?
Question 12
Analyze the code below. What is the issue?
import random
t = (1, 2, 3)
random.shuffle(t)Question 13
Which function allows sampling *with* replacement (duplicates allowed)?
Question 14
Analyze the code below. What does `k=5` specify?
random.choices(['red', 'black'], weights=[18, 18], k=5)Question 15
Why should you NOT use the `random` module for generating passwords or tokens?
Question 16
What does `random.randrange(0, 10, 2)` generate?
Question 17
Analyze the code below. What is the output?
import random
print(random.randint(5, 5))Question 18
Which distribution function returns a random number from a Gaussian (normal) distribution?
Question 19
What happens if you use `random.sample()` with a `k` larger than the population size?
Question 20
Analyze the code below. What does `random.random() * 5` produce?
val = random.random() * 5Question 21
How can you save and restore the state of the random number generator?
Question 22
What is the default seed if you don't call `random.seed()`?
Question 23
Analyze the code below. What is the return type?
import random
x = random.choice("Hello")Question 24
Which function would you use to simulate a weighted coin toss (e.g., 70% heads, 30% tails)?
Question 25
What does `random.triangular(low, high, mode)` do?
Question 26
Analyze the code below. What is the output?
import random
print(random.randrange(10))Question 27
Is the `random` module thread-safe?
Question 28
How do you generate a random integer with `k` bits?
Question 29
Analyze the code below. What is the result?
import random
lst = [1, 2, 3]
random.shuffle(lst)
print(lst)Question 30
Which method is used to instantiate a new random number generator isolated from the global one?
Question 31
What is the range of `random.uniform(1.5, 1.5)`?
Question 32
Analyze the code below. What is the output?
import random
random.seed("hello")
print(random.randint(1, 10))Question 33
Which function would you use to shuffle a list and return a *new* list without modifying the original?
Question 34
What is the underlying algorithm used by Python's `random` module?
Question 35
Analyze the code below. What is the result?
import random
print(random.choice({'a': 1, 'b': 2}))