Pick a Random Region with Mock Carbon Intensity in Python
Selects a random region from a list and generates a mock carbon intensity value using Python's random module.
Python code
13 linesimport random
def pick_region_intensity(regions, seed=42):
random.seed(seed)
selected = random.choice(regions)
intensity = random.randint(1, 10)
return selected, intensity
if __name__ == "__main__":
regions = ["North", "South", "East", "West"]
selected, intensity = pick_region_intensity(regions)
print(f"Selected region: {selected}")
print(f"Mock carbon intensity: {intensity} units")
Output
Selected region: South
Mock carbon intensity: 7 units
How it works
The function uses random.seed() to make results reproducible, which is useful for testing or demos. random.choice() picks one item uniformly from the list, while random.randint(1, 10) returns a random integer between 1 and 10 inclusive. Seeding before each call ensures consistent output across runs with the same seed. This pattern is common in cloud demos where deterministic placeholders are needed.
Common mistakes
- Forgetting to call random.seed() can lead to non-reproducible outputs in tests.
- Assuming the mock intensity is realistic — it's random, not aligned with actual carbon data.
- Using random.random() instead of random.randint() when an integer range is required.
Variations
- Use numpy.random.choice for weighted region selection.
- Replace randint with a range lookup based on historical carbon data.
Real-world use cases
- Generating sample data for cloud dashboard mockups before real telemetry exists.
- Simulating regional load variations in a microservices demo for capacity planning.
- Creating deterministic test fixtures for a carbon-aware scheduling application.
Sponsored
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
Keep learning
Related tutorials and quizzes for this topic.