Random Integers Practice Problem
This data science coding problem helps you practice Random Sampling & Generators, random integers, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Random Sampling & Generators.
- Problem ID: 116
- Problem key: 116-random-integers
- URL: https://datacrack.app/solve/116-random-integers
- Difficulty: easy
- Topic: Random Sampling & Generators
- Module: NumPy Foundations
Problem Statement
# 🧩 Random Integers
---
### 🎯 Goal
Random sampling is the starting point for simulations, experiments, and stochastic algorithms. `np.random.randint` generates integers from a **discrete uniform distribution** — every value in `[low, high)` has equal probability. This problem teaches you to generate random integers, set a seed for reproducibility, and summarize simulated results.
---
### 🔍 Random Integers with `np.random.randint`
```python
np.random.seed(42) # reproducibility
np.random.randint(0, 10, size=5) # 5 random ints in [0, 10)
np.random.randint(1, 7, size=(3, 4)) # 3×4 matrix of dice rolls
```
| Parameter | Meaning |
|:----------|:--------|
| `low` | Inclusive lower bound |
| `high` | **Exclusive** upper bound |
| `size` | Output shape (int or tuple) |
**Mean of discrete uniform on {low, ..., high-1}:** $(low + high - 1) / 2$
---
### 💻 Task
Implement `generate_random_ints(low, high, size, seed)` that generates random integers and returns `[n_unique, round(mean, 1)]`.
---
### 📥 Input
- `low`: int — inclusive lower bound
- `high`: int — exclusive upper bound
- `size`: int — number of samples to generate
- `seed`: int — random seed for reproducibility
### 📤 Output
- List: `[number_of_unique_values, rounded_mean]`
---
### 🧩 Starter Code
```python
import numpy as np
def generate_random_ints(low, high, size, seed):
"""
Generate random integers and return distribution summary.
Args:
low (int): Inclusive lower bound
high (int): Exclusive upper bound
size (int): Number of samples
seed (int): Random seed
Returns:
list: [n_unique_values, rounded_mean]
"""
# 🧠 TODO: Set seed with np.random.seed(seed)
# 🧠 TODO: Generate array with np.random.randint(low, high, size=size)
# 🧠 TODO: Compute n_unique and round(mean, 1)
pass
```
---
### 💡 Example
```python
generate_random_ints(0, 10, 100000, 42)
# Expected: [10, 4.5] — all 10 values appear, mean ≈ (0+9)/2
```
---
### 🔑 Key Concepts
- `np.random.seed()` makes random generation **reproducible** — same seed = same output
- `high` is **exclusive**: `randint(0, 10)` generates values 0–9, never 10
- With enough samples, all values in `[low, high)` appear and mean converges to the theoretical valueStarter Code
import numpy as np
def generate_random_ints(low, high, size, seed):
"""
Generate random integers and return distribution summary.
Args:
low (int): Inclusive lower bound
high (int): Exclusive upper bound
size (int): Number of samples
seed (int): Random seed
Returns:
list: [n_unique_values, rounded_mean]
"""
# 🧠 TODO: Set seed with np.random.seed(seed)
# 🧠 TODO: Generate array with np.random.randint(low, high, size=size)
# 🧠 TODO: Compute n_unique and round(mean, 1)
pass