Jumping the State Practice Problem
This data science coding problem helps you practice Random Sampling & Generators, jumping the state, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Random Sampling & Generators.
- Problem ID: 111
- Problem key: 111-jumping-the-state
- URL: https://datacrack.app/solve/111-jumping-the-state
- Difficulty: medium
- Topic: Random Sampling & Generators
- Module: NumPy Foundations
Problem Statement
# 🧩 Jumping the State
---
### 🎯 Goal
Some BitGenerators, like `PCG64`, can create a new BitGenerator whose state is jumped far ahead. This is useful when you want separate deterministic streams from one seed.
Unlike `spawn()`, which creates child generators, `jumped()` moves along the same BitGenerator sequence by a very large fixed step.
---
### 🔍 Example
```python
bg = np.random.PCG64(42)
bg_jumped = bg.jumped()
rng2 = np.random.Generator(bg_jumped)
```
> **Warning:** Do not reuse overlapping jumped streams. If you create jumped streams manually, track which jump positions you already used.
---
### 💻 Task
Implement `jump_generator_state(seed, size)` that initializes a PCG64 BitGenerator, gets `size` random floats, jumps the state, creates a new Generator from the jumped state, and gets another `size` random floats.
---
### 📥 Input
- `seed`: int
- `size`: int
### 📤 Output
- A list containing two lists `[first_floats, jumped_floats]`.
---
### 🧩 Starter Code
```python
import numpy as np
def jump_generator_state(seed, size):
"""
Jump a PCG64 generator state.
"""
# 🧠 TODO: Create PCG64 and first Generator
# 🧠 TODO: Get first array of floats
# 🧠 TODO: Call .jumped() on the BitGenerator
# 🧠 TODO: Create second Generator and get second array
# 🧠 TODO: Return [first, second]
pass
```
---
### 💡 Expected Output
```python
jump_generator_state(42, 2)
# Expected: [[0.773..., 0.438...], [0.508..., 0.314...]]
```
---
### 🔑 Key Concepts
- `jumped()` returns a **new BitGenerator** with its state moved far ahead
- The original BitGenerator is not modified
- For `PCG64`, one jump is as if a huge fixed number of random values had already been generated
- `jumped()` is useful for deterministic stream splitting, but `spawn()` is usually the simpler choice for parallel workers
Starter Code
import numpy as np
def jump_generator_state(seed, size):
"""
Jump a PCG64 generator state.
"""
# 🧠 TODO: Create PCG64 and first Generator
# 🧠 TODO: Get first array of floats
# 🧠 TODO: Call .jumped() on the BitGenerator
# 🧠 TODO: Create second Generator and get second array
# 🧠 TODO: Return [first, second]
pass