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.

Easy Python 3.9+ Aug 9, 2026 Cloud + Python 14 views 0 copies

Python code

13 lines
Python 3.9+
import 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

stdout
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

  1. Use numpy.random.choice for weighted region selection.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Cloud + Python

Related tutorials and quizzes for this topic.