Seeded vs Unseeded Randomness Practice Problem
This data science coding problem helps you practice Random Sampling & Generators, seeded vs unseeded randomness, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Random Sampling & Generators.
- Problem ID: 118
- Problem key: 118-seeded-vs-unseeded-randomness
- URL: https://datacrack.app/solve/118-seeded-vs-unseeded-randomness
- Difficulty: easy
- Topic: Random Sampling & Generators
- Module: NumPy Foundations
Problem Statement
# 🧩 Seeded vs Unseeded Randomness
---
### 🎯 Goal
Randomness in computing is pseudo-random. By setting the same **seed**, you reset the generator to its starting point and get the exact same sequence. If you don't reset the seed, the generator keeps moving forward, giving you a different sequence. This problem teaches the difference between resetting the seed before each call versus letting the generator continue.
---
### 🔍 How Seeds Work
```python
np.random.seed(42)
first = np.random.random(5)
# Resetting the same seed gives the same sequence
np.random.seed(42)
second = np.random.random(5)
print(np.array_equal(first, second)) # True
# Without resetting, the generator keeps moving forward
third = np.random.random(5)
print(np.array_equal(second, third)) # False
```
---
### 💻 Task
Implement `compare_seeded_randomness(seed, size)` that:
- generates two arrays after resetting the same seed before each one
- generates two arrays without resetting the seed between them
- returns `[seeded_same, unseeded_same]`
---
### 📥 Input
- `seed`: int — random seed to use
- `size`: int — number of random values to generate
### 📤 Output
- List `[seeded_same, unseeded_same]` containing boolean results
---
### 🧩 Starter Code
```python
import numpy as np
def compare_seeded_randomness(seed, size):
"""
Compare resetting the seed vs not resetting the seed.
Args:
seed (int): Random seed
size (int): Number of random values to generate
Returns:
list: [seeded_same, unseeded_same]
"""
# 🧠 TODO: Show what happens when the same seed is reset
# 🧠 TODO: Show what happens when the seed is not reset
# 🧠 TODO: Return both comparison results
pass
```
---
### 💡 Example
```python
compare_seeded_randomness(42, 5)
# Expected: [True, False]
```
---
### 🔑 Key Concepts
- **same seed reset → same sequence**: `np.random.seed(n)` resets the PRNG to the same initial state.
- **no reset → generator keeps moving forward → different sequence**: consecutive calls to random functions usually produce different values because the generator state advances.
- `np.array_equal(a, b)` checks whether two arrays are exactly identical
- `np.allclose(a, b)` checks whether two arrays are approximately equal, useful when tiny floating-point differences are expected
Starter Code
import numpy as np
def compare_seeded_randomness(seed, size):
"""
Compare resetting the seed vs not resetting the seed.
Args:
seed (int): Random seed
size (int): Number of random values to generate
Returns:
list: [seeded_same, unseeded_same]
"""
# 🧠 TODO: Show what happens when the same seed is reset
# 🧠 TODO: Show what happens when the seed is not reset
# 🧠 TODO: Return both comparison results
pass