Generators vs BitGenerators Practice Problem
This data science coding problem helps you practice Random Sampling & Generators, generators vs bitgenerators, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Random Sampling & Generators.
- Problem ID: 110
- Problem key: 110-generators-vs-bitgenerators
- URL: https://datacrack.app/solve/110-generators-vs-bitgenerators
- Difficulty: medium
- Topic: Random Sampling & Generators
- Module: NumPy Foundations
Problem Statement
# 🧩 Generators vs BitGenerators
---
### 🎯 Goal
NumPy’s modern random API has two layers:
- **BitGenerator**: the low-level algorithm that produces raw random bits
- **Generator**: the user-facing object that turns those bits into useful distributions like uniform, normal, integers, and choice
Most of the time, `np.random.default_rng(seed)` is enough. This lesson shows what is happening underneath by manually connecting a BitGenerator to a Generator.
---
### 🔍 BitGenerator + Generator
```python
# SFC64 is the low-level BitGenerator.
# It is the algorithm that produces raw random bits.
bg = np.random.SFC64(42)
# Generator is the high-level object.
# It turns those bits into useful random values.
rng = np.random.Generator(bg)
# Generate 3 random floats from the Generator.
print(rng.random(3))
```
---
### 💻 Task
Implement `fast_generator_sfc64(seed, size)` that explicitly creates an SFC64 BitGenerator with the given seed, passes it to a Generator, and returns `size` random floats.
---
### 📥 Input
- `seed`: int
- `size`: int
### 📤 Output
- List of random floats generated via SFC64.
---
### 🧩 Starter Code
```python
import numpy as np
def fast_generator_sfc64(seed, size):
"""
Initialize a Generator using the SFC64 BitGenerator.
"""
# 🧠 TODO: Create SFC64 BitGenerator
# 🧠 TODO: Create Generator
# 🧠 TODO: Generate floats and return as list
pass
```
---
### 💡 Expected Output
```python
fast_generator_sfc64(42, 3)
# Expected: [0.529..., 0.378..., 0.945...]
```
---
### 🔑 Key Concepts
- A `BitGenerator` is the low-level random algorithm, such as `PCG64`, `SFC64`, or `MT19937`
- A `Generator` is the high-level interface that provides methods like `.random()`, `.normal()`, `.integers()`, and `.choice()`
- `np.random.default_rng(seed)` creates a `Generator` for you automatically
- You usually do not need to choose a BitGenerator manually unless you are doing advanced reproducibility or performance experiments
Starter Code
import numpy as np
def fast_generator_sfc64(seed, size):
"""
Initialize a Generator using the SFC64 BitGenerator.
"""
# 🧠 TODO: Create SFC64 BitGenerator
# 🧠 TODO: Create Generator
# 🧠 TODO: Generate floats and return as list
pass