analytics: Do not reseed the global random generator.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-07-31 16:42:29 -07:00 committed by Tim Abbott
parent ea83c911e9
commit 710d1f7f51
1 changed files with 7 additions and 6 deletions

View File

@ -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 = [