Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

analytics: Do not reseed the global random generator.

Anders Kaseorg710d1f7f518b
diff --git a/analytics/lib/fixtures.py b/analytics/lib/fixtures.py
index 12eed78d01e..17168110c10 100644
--- a/analytics/lib/fixtures.py
+++ b/analytics/lib/fixtures.py
@@ -1,5 +1,5 @@
from math import sqrt
-from random import gauss, random, seed
+from random import Random
from typing import List
from analytics.lib.counts import CountStat
@@ -36,6 +36,8 @@ def generate_time_series_data(
partial_sum -- If True, return partial sum of the series.
random_seed -- Seed for random number generator.
"""
+ rng = Random(random_seed)
+
if frequency == CountStat.HOUR:
length = days * 24
seasonality = [non_business_hours_base] * 24 * 7
@@ -44,13 +46,13 @@ def generate_time_series_data(
seasonality[24 * day + hour] = business_hours_base
holidays = []
for i in range(days):
- holidays.extend([random() < holiday_rate] * 24)
+ holidays.extend([rng.random() < holiday_rate] * 24)
elif frequency == CountStat.DAY:
length = days
seasonality = [8 * business_hours_base + 16 * non_business_hours_base] * 5 + [
24 * non_business_hours_base
] * 2
- holidays = [random() < holiday_rate for i in range(days)]
+ holidays = [rng.random() < holiday_rate for i in range(days)]
else:
raise AssertionError(f"Unknown frequency: {frequency}")
if length < 2:
@@ -62,11 +64,10 @@ def generate_time_series_data(
seasonality[i % len(seasonality)] * (growth_base**i) for i in range(length)
]
- seed(random_seed)
- noise_scalars = [gauss(0, 1)]
+ noise_scalars = [rng.gauss(0, 1)]
for i in range(1, length):
noise_scalars.append( # noqa: PERF401 # https://github.com/astral-sh/ruff/issues/6210
- noise_scalars[-1] * autocorrelation + gauss(0, 1) * (1 - autocorrelation)
+ noise_scalars[-1] * autocorrelation + rng.gauss(0, 1) * (1 - autocorrelation)
)
values = [