From ce039d69ba0ccc31ea504f62d55e33d6bdb46188 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Fri, 24 Apr 2026 22:02:59 +0700 Subject: [PATCH] saip/param_source: try to not repeat random values Change-Id: I4fa743ef5677580f94b9df16a5051d1d178edeb0 Jenkins: skip-card-test --- pySim/esim/saip/param_source.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pySim/esim/saip/param_source.py b/pySim/esim/saip/param_source.py index 7caf8484..af00bb70 100644 --- a/pySim/esim/saip/param_source.py +++ b/pySim/esim/saip/param_source.py @@ -130,15 +130,23 @@ class RandomDigitSource(DecimalRangeSource, RandomSourceMixin): """return a different sequence of random decimal digits each""" name = "random decimal digits" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.used_keys = set() + def get_next(self, csv_row:dict=None): - val = self.random_impl.randint(self.first_value, self.last_value) + # try to generate random digits that are always different from previously produced random digits + for _ in range(10): + val = self.random_impl.randint(self.first_value, self.last_value) + if val not in self.used_keys: + break + self.used_keys.add(val) return self.val_to_digit(val) class RandomHexDigitSource(InputExpandingParamSource, RandomSourceMixin): """return a different sequence of random hexadecimal digits each""" name = "random hexadecimal digits" numeric_base = 16 - def __init__(self, input_str:str): super().__init__(input_str) input_str = self.input_str @@ -150,9 +158,16 @@ class RandomHexDigitSource(InputExpandingParamSource, RandomSourceMixin): if (num_digits & 1) != 0: raise ValueError(f"hexadecimal value should have even number of digits, not {num_digits}") self.num_digits = num_digits + self.used_keys = set() def get_next(self, csv_row:dict=None): - val = self.random_impl.randbytes(self.num_digits // 2) + # try to generate random bytes that are always different from previously produced random bytes + for _ in range(10): + val = self.random_impl.randbytes(self.num_digits // 2) + if val not in self.used_keys: + break + self.used_keys.add(val) + return b2h(val) class IncDigitSource(DecimalRangeSource):