Pseudo-Random Distributions with `random.gauss` & `random.expovariate`
Beyond uniform numbers, Python's `random` module samples real-world distributions: `random.gauss(mu, sigma)` creates bell curves, while `random.expovariate(lambd)` models inter-arrival times for Poisson request streams 🎲.
import random
random.seed(42)
# Gaussian: natural variance, network latency, measurement noise
latencies = [random.gauss(100.0, 15.0) for _ in range(3)]
print(f"Latencies (ms): {[round(x, 1) for x in latencies]} 🔔")
# Exponential: waiting times between server hits (lambda = rate)
intervals = [random.expovariate(0.5) for _ in range(3)]
print(f"Arrival intervals (s): {[round(t, 2) for t in intervals]} ⏱️")
# 57. Pseudo-Random Distributions with `random.gauss` & `random.expovariate` Beyond uniform numbers, Python's `random` module samples real-world distributions: `random.gauss(mu, sigma)` creates bell curves, while `random.expovariate(lambd)` models inter-arrival times for Poisson request streams 🎲. ```python import random random.seed(42) # Gaussian: natural variance, network latency, measurement noise latencies = [random.gauss(100.0, 15.0) for _ in range(3)] print(f"Latencies (ms): {[round(x, 1) for x in latencies]} 🔔") # Exponential: waiting times between server hits (lambda = rate) intervals = [random.expovariate(0.5) for _ in range(3)] print(f"Arrival intervals (s): {[round(t, 2) for t in intervals]} ⏱️") ``` For exponential distributions, the expected average value is $1/\lambda$. If your API handles 2 requests per second ($\lambda = 2.0$), the mean delay between requests is $0.5$ seconds ⚡. --- ### Question What physical real-world process is typically modeled using `random.expovariate(lambd)`? 🧠 - [ ] a. Time intervals between independent Poisson events such as web server request arrivals ⏱️ - [ ] b. Unbiased two-outcome coin flips mapped to binary bytes - [ ] c. Discrete uniform integers picked without replacement from an array - [ ] d. Cryptographic SHA-256 salt collisions across microservices --- ### Challenge Seed random with `1337`. Generate 3 samples from `random.expovariate(2.0)`. Print the list with values rounded to 2 decimal places. 🎯 <details> <summary>Solution</summary> ```python import random random.seed(1337) vals = [round(random.expovariate(2.0), 2) for _ in range(3)] print(vals) ``` </details>