Random Floats Practice Problem
This data science coding problem helps you practice Random Sampling & Generators, random floats, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Random Sampling & Generators.
- Problem ID: 115
- Problem key: 115-random-floats
- URL: https://datacrack.app/solve/115-random-floats
- Difficulty: easy
- Topic: Random Sampling & Generators
- Module: NumPy Foundations
Problem Statement
# 🎲 Random Floats & Continuous Sampling
---
### 🎯 Goal
`np.random.uniform` generates floating-point numbers from a **continuous uniform distribution** on `[low, high)`. This is the foundation for Monte Carlo methods, random initialization in ML, and generating synthetic data. Understanding the statistical properties (mean and standard deviation) of uniform samples is essential for validation.
---
### 🔍 Random Floats with `np.random.uniform`
```python
np.random.seed(42)
np.random.uniform(0, 1, size=5) # 5 floats in [0, 1)
np.random.uniform(-1, 1, size=(3, 3)) # 3×3 matrix in [-1, 1)
```
**Theoretical properties of Uniform(low, high):**
| Property | Formula |
|:---------|:--------|
| Mean | $(low + high) / 2$ |
| Std Dev | $(high - low) / \sqrt{12}$ |
---
### 💻 Task
Implement `generate_random_floats(low, high, size, seed)` that generates uniform random floats and returns `[round(mean, 1), round(std, 1)]`.
---
### 📥 Input
- `low`: float — lower bound
- `high`: float — upper bound
- `size`: int — number of samples
- `seed`: int — random seed
### 📤 Output
- List: `[rounded_mean, rounded_std]`
---
### 🧩 Starter Code
```python
import numpy as np
def generate_random_floats(low, high, size, seed):
"""
Generate uniform random floats and return summary statistics.
Args:
low (float): Lower bound
high (float): Upper bound
size (int): Number of samples
seed (int): Random seed
Returns:
list: [rounded_mean, rounded_std]
"""
# 🧠 TODO: Set seed, generate uniform samples
# 🧠 TODO: Compute and return [round(mean, 1), round(std, 1)]
pass
```
---
### 💡 Example
```python
generate_random_floats(0.0, 10.0, 100000, 42)
# Expected: [5.0, 2.9] — mean=(0+10)/2, std=10/√12≈2.89
```
---
### 🔑 Key Concepts
- `np.random.uniform(low, high, size)` — continuous uniform distribution
- `np.random.random(size)` is shorthand for `uniform(0, 1, size)`
- With large samples, sample mean and std converge to the theoretical values (Law of Large Numbers)Starter Code
import numpy as np
def generate_random_floats(low, high, size, seed):
"""
Generate uniform random floats and return summary statistics.
Args:
low (float): Lower bound
high (float): Upper bound
size (int): Number of samples
seed (int): Random seed
Returns:
list: [rounded_mean, rounded_std]
"""
# 🧠 TODO: Set seed, generate uniform samples
# 🧠 TODO: Compute and return [round(mean, 1), round(std, 1)]
pass