0
0
Pythonprogramming~20 mins

Random data generation in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Random Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of random integer generation
What is the output of this code snippet?
Python
import random
random.seed(10)
print(random.randint(1, 10))
A5
B7
C1
D10
Attempts:
2 left
💡 Hint
The seed fixes the random number sequence.
Predict Output
intermediate
2:00remaining
Output of random choice from list
What is the output of this code snippet?
Python
import random
random.seed(3)
colors = ['red', 'green', 'blue']
print(random.choice(colors))
Ared
Bblue
Cgreen
DIndexError
Attempts:
2 left
💡 Hint
Seed controls the choice result.
Predict Output
advanced
2:00remaining
Output of random.sample with repeated elements
What is the output of this code snippet?
Python
import random
random.seed(1)
items = [1, 2, 2, 3, 4]
sample = random.sample(items, 3)
print(sample)
AValueError
B[2, 1, 4]
C[2, 3, 4]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
random.sample picks unique indices, not unique values.
Predict Output
advanced
2:00remaining
Output of random.choices with weights
What is the output of this code snippet?
Python
import random
random.seed(0)
items = ['a', 'b', 'c']
weights = [10, 1, 1]
result = random.choices(items, weights=weights, k=5)
print(result)
A['a', 'a', 'b', 'a', 'a']
B['a', 'a', 'a', 'a', 'a']
C['b', 'a', 'a', 'c', 'a']
DTypeError
Attempts:
2 left
💡 Hint
Weights influence probability but do not guarantee all same picks.
🧠 Conceptual
expert
2:00remaining
Effect of random.seed on random.random() sequence
Which option correctly describes the output of this code snippet?
Python
import random
random.seed(42)
values = [round(random.random(), 3) for _ in range(4)]
print(values)
A[0.639, 0.025, 0.275, 0.736]
B[0.639, 0.025, 0.275, 0.223, 0.736]
C[0.639, 0.025, 0.275, 0.223]
DTypeError
Attempts:
2 left
💡 Hint
random.seed fixes the sequence of random.random() calls.