Random Arrays with Seed Practice Problem
This data science coding problem helps you practice Array Creation & Attributes, random arrays with seed, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Array Creation & Attributes.
- Problem ID: 47
- Problem key: 47-random-arrays-with-seed
- URL: https://datacrack.app/solve/47-random-arrays-with-seed
- Difficulty: easy
- Topic: Array Creation & Attributes
- Module: NumPy Foundations
Problem Statement
# 🧩 Random Arrays with Seed
---
### 🎯 Goal
Random number generation is everywhere in data science: initializing model weights, splitting train/test sets, bootstrapping, Monte Carlo simulations. The **seed** makes randomness **reproducible** — you get the same "random" numbers every time, enabling reproducible experiments.
---
### 🔍 Reproducibility with Seeds
```python
np.random.seed(42)
np.random.rand(3) # → [0.3745..., 0.9507..., 0.7319...]
np.random.seed(42) # reset seed
np.random.rand(3) # → [0.3745..., 0.9507..., 0.7319...] (same!)
```
Without a seed, you get different numbers every run — makes debugging and experiment comparison impossible.
---
### 💻 Task
Implement `random_array(seed, shape, distribution)`.
---
### 📥 Input
- `seed`: int — the random seed
- `shape`: list — e.g., `[3]` for 1D, `[2, 3]` for 2D
- `distribution`: string — `"uniform"` (values in [0,1)) or `"normal"` (mean=0, std=1)
### 📤 Output
- The random array as a Python list
---
### 🧩 Starter Code
```python
import numpy as np
def random_array(seed, shape, distribution):
"""
Generate a reproducible random NumPy array.
Args:
seed (int): Random seed for reproducibility
shape (list): Array shape
distribution (str): "uniform" or "normal"
Returns:
list: Random array as a Python list
"""
np.random.seed(seed)
# 🧠 TODO
pass
```
---
### 🔑 Key Concepts
- `np.random.seed(n)` — sets the global seed before generation
- `np.random.rand(*shape)` — uniform distribution [0, 1)
- `np.random.randn(*shape)` — standard normal distribution N(0, 1)
- `*shape` unpacks the list as positional arguments: `rand(*[3,4])` = `rand(3, 4)`Starter Code
import numpy as np
def random_array(seed, shape, distribution):
"""
Generate a reproducible random NumPy array.
Args:
seed (int): Random seed for reproducibility
shape (list): Array shape
distribution (str): "uniform" or "normal"
Returns:
list: Random array as a Python list
"""
np.random.seed(seed)
# 🧠 TODO
pass